首页 > 编程 > Java > 正文

SQLite - Java

2019-11-06 06:03:59
字体:
来源:转载
供稿:网友
     在 java 程序中使用 SQLite 之前,我们需要确保机器上已经有 SQLite JDBC Driver 驱动程序和 Java。可以查看 Java 教程了解如何在计算机上安装 Java。现在,我们来看看如何在机器上安装 SQLite JDBC 驱动程序。    从 sqlite-jdbc库下载 sqlite-jdbc-(VERSION).jar 的最新版本。    在您的 class 路径中添加下载的 jar 文件 sqlite-jdbc-(VERSION).jar,或者在 -classpath 选项中使用它,这将在后面的实例中进行讲解。

    在学习下面部分的知识之前,您必须对 Java JDBC 概念有初步了解。如果您还未了解相关知识,那么建议您可以先花半个小时学习下 JDBC 教程相关知识,这将有助于您学习接下来讲解的知识。

package sqliteDemo;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class sqliteTest {        PRivate static Connection c = null;    private static Statement stmt = null;        /**     * @param args     */    public static void main(String[] args) {        try {          Class.forName("org.sqlite.JDBC");                c = DriverManager.getConnection("jdbc:sqlite:test.db");//如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象。          System.out.println("Opened database successfully");             createdTable();          //insert();          //select();          //update();          //delete();                    stmt.close();          c.close();        } catch ( Exception e ) {          System.err.println( e.getClass().getName() + ": " + e.getMessage() );          System.exit(0);        }           }            public static void createdTable() throws SQLException{          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);          System.out.println("Table created successfully");    }            public static void insert() throws SQLException{          c.setAutoCommit(false);          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);          c.commit();          stmt.close();          System.out.println("Insert successfully");    }            public static void select() throws SQLException{          c.setAutoCommit(false);          stmt = c.createStatement();          ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );          while ( rs.next() ) {             int id = rs.getInt("id");             String  name = rs.getString("name");             int age  = rs.getInt("age");             String  address = rs.getString("address");             float salary = rs.getFloat("salary");             System.out.print( "ID = " + id );             System.out.print( "NAME = " + name );             System.out.print( "AGE = " + age );             System.out.print( "ADDRESS = " + address );             System.out.println( "SALARY = " + salary );          }          rs.close();    }            public static void update() throws SQLException{          c.setAutoCommit(false);          stmt = c.createStatement();          String sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=2;";          stmt.executeUpdate(sql);          c.commit();          ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );          while ( rs.next() ) {             int id = rs.getInt("id");             String  name = rs.getString("name");             int age  = rs.getInt("age");             String  address = rs.getString("address");             float salary = rs.getFloat("salary");             System.out.print( "ID = " + id );             System.out.print( "NAME = " + name );             System.out.print( "AGE = " + age );             System.out.print( "ADDRESS = " + address );             System.out.println( "SALARY = " + salary );          }          rs.close();                }        public static void delete() throws SQLException{          c.setAutoCommit(false);          stmt = c.createStatement();          String sql = "DELETE from COMPANY where ID=2;";          stmt.executeUpdate(sql);          c.commit();          ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );          while ( rs.next() ) {             int id = rs.getInt("id");             String  name = rs.getString("name");             int age  = rs.getInt("age");             String  address = rs.getString("address");             float salary = rs.getFloat("salary");             System.out.println( "ID = " + id );             System.out.println( "NAME = " + name );             System.out.println( "AGE = " + age );             System.out.println( "ADDRESS = " + address );             System.out.println( "SALARY = " + salary );             System.out.println();          }          rs.close();    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表