美文网首页
sqlite(18)-java

sqlite(18)-java

作者: Flannery | 来源:发表于2016-06-15 18:01 被阅读0次

    连接数据库#

    import java.sql.*;
    public class SQliteJDBC
    {
      public static void main(String args[])
      {
        Connection c = null;
        try{
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:test.db");
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ":" + e.getMessage());
            System.exit(0);
        }
        System.out.println("Opened database successfully");
      }
    }
    

    创建表#

    import java.sql.*;
    public class SQLiteJDBC
    {
      public static void main(String args[])
      {
        Connection c = null;
        Statement stmt = null;
        try{
        Class.forName("org.sqlite.JDBC");
        c = DriverManger.getConnection("jdbc:sqlite:test.db");
        System.out.println("Opened database successfully");
        stmt = c.createStatement();
        String sql = "CREATE TABLE COMPANY " +
                     "(ID INT PRIMARY KEY NOT NULL, " + 
                     "NAME TEXT NOT NULL," +
                     "AGE INT NOT NULL, " +
                     "ADDRESS CHAR(50), "+
                     "SALARY REAL)";
        stmt.executeUpdate(sql);
        stmt.close();
        c.close();
        } catch(Exception e) {
          System.err.println(e.getClass().getName() + ":" + e.getMessage() );
        System.exit(0);
        }
        System.out.println("Table created successfully");
      }
    }
    

    INSERT操作#

    import java.sql.*;
    publc class SQLiteJDBC
    {
      Connection c = null;
      Statement stmt = null;
      try{
        Class.forName("org.sqlite.JDBC"); 
        c = DriverManager.getConnection("jdbc:sqlite:test.db");
         c.setAutoCommit(false); 
        System.out.println("Opened database successfully"); 
        stmt = c.createStatement();
         String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (1, 'Paul', 32, 'California', 20000.00 );";
         stmt.executeUpdate(sql); 
        sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +     "VALUES (2, 'Allen', 25, 'Texas', 15000.00 );";
        stmt.executeUpdate(sql); sql = "INSERT INTO COMPANY   (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );"; 
        stmt.executeUpdate(sql); 
        sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );"; 
        stmt.executeUpdate(sql); stmt.close(); c.commit(); c.close();
      } catch(){
      }
    }
    

    SELECT操作#

    SELECT

    UPDATE操作#

    UPDATE

    DELETE操作#

    DELETE

    相关文章

      网友评论

          本文标题:sqlite(18)-java

          本文链接:https://www.haomeiwen.com/subject/mrkpdttx.html