SQLite – Java

安装

在 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 教程相关知识,这将有助于您学习接下来讲解的知识。

连接数据库

下面的 Java 程序显示了如何连接到一个现有的数据库。如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象。

  1. import java.sql.*;
  2. public class SQLiteJDBC
  3. {
  4. public static void main( String args[] )
  5. {
  6. Connection c = null;
  7. try {
  8. Class.forName("org.sqlite.JDBC");
  9. c = DriverManager.getConnection("jdbc:sqlite:test.db");
  10. } catch ( Exception e ) {
  11. System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  12. System.exit(0);
  13. }
  14. System.out.println("Opened database successfully");
  15. }
  16. }

现在,让我们来编译和运行上面的程序,在当前目录中创建我们的数据库 test.db。您可以根据需要改变路径。我们假设当前路径下可用的 JDBC 驱动程序的版本是 sqlite-jdbc-3.7.2.jar

  1. $javac SQLiteJDBC.java
  2. $java -classpath ".:sqlite-jdbc-3.7.2.jar" SQLiteJDBC
  3. Open database successfully

如果您想要使用 Windows 机器,可以按照下列所示编译和运行您的代码:

  1. $javac SQLiteJDBC.java
  2. $java -classpath ".;sqlite-jdbc-3.7.2.jar" SQLiteJDBC
  3. Opened database successfully

创建表

下面的 Java 程序将用于在先前创建的数据库中创建一个表:

  1. import java.sql.*;
  2. public class SQLiteJDBC
  3. {
  4. public static void main( String args[] )
  5. {
  6. Connection c = null;
  7. Statement stmt = null;
  8. try {
  9. Class.forName("org.sqlite.JDBC");
  10. c = DriverManager.getConnection("jdbc:sqlite:test.db");
  11. System.out.println("Opened database successfully");
  12. stmt = c.createStatement();
  13. String sql = "CREATE TABLE COMPANY " +
  14. "(ID INT PRIMARY KEY NOT NULL," +
  15. " NAME TEXT NOT NULL, " +
  16. " AGE INT NOT NULL, " +
  17. " ADDRESS CHAR(50), " +
  18. " SALARY REAL)";
  19. stmt.executeUpdate(sql);
  20. stmt.close();
  21. c.close();
  22. } catch ( Exception e ) {
  23. System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  24. System.exit(0);
  25. }
  26. System.out.println("Table created successfully");
  27. }
  28. }

上述程序编译和执行时,它会在 test.db 中创建 COMPANY 表,最终文件列表如下所示:

  1. -rw-r--r--. 1 root root 3201128 Jan 22 19:04 sqlite-jdbc-3.7.2.jar
  2. -rw-r--r--. 1 root root 1506 May 8 05:43 SQLiteJDBC.class
  3. -rw-r--r--. 1 root root 832 May 8 05:42 SQLiteJDBC.java
  4. -rw-r--r--. 1 root root 3072 May 8 05:43 test.db

INSERT 操作

下面的 Java 代码显示了如何在上面创建的 COMPANY 表中创建记录:

  1. import java.sql.*;
  2. public class SQLiteJDBC
  3. {
  4. public static void main( String args[] )
  5. {
  6. Connection c = null;
  7. Statement stmt = null;
  8. try {
  9. Class.forName("org.sqlite.JDBC");
  10. c = DriverManager.getConnection("jdbc:sqlite:test.db");
  11. c.setAutoCommit(false);
  12. System.out.println("Opened database successfully");
  13. stmt = c.createStatement();
  14. String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
  15. "VALUES (1, 'Paul', 32, 'California', 20000.00 );";
  16. stmt.executeUpdate(sql);
  17. sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
  18. "VALUES (2, 'Allen', 25, 'Texas', 15000.00 );";
  19. stmt.executeUpdate(sql);
  20. sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
  21. "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );";
  22. stmt.executeUpdate(sql);
  23. sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
  24. "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
  25. stmt.executeUpdate(sql);
  26. stmt.close();
  27. c.commit();
  28. c.close();
  29. } catch ( Exception e ) {
  30. System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  31. System.exit(0);
  32. }
  33. System.out.println("Records created successfully");
  34. }
  35. }

上述程序编译和执行时,它会在 COMPANY 表中创建给定记录,并会显示以下两行:

  1. Opened database successfully
  2. Records created successfully

SELECT 操作

下面的 Java 程序显示了如何从前面创建的 COMPANY 表中获取并显示记录:

  1. import java.sql.*;
  2. public class SQLiteJDBC
  3. {
  4. public static void main( String args[] )
  5. {
  6. Connection c = null;
  7. Statement stmt = null;
  8. try {
  9. Class.forName("org.sqlite.JDBC");
  10. c = DriverManager.getConnection("jdbc:sqlite:test.db");
  11. c.setAutoCommit(false);
  12. System.out.println("Opened database successfully");
  13. stmt = c.createStatement();
  14. ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
  15. while ( rs.next() ) {
  16. int id = rs.getInt("id");
  17. String name = rs.getString("name");
  18. int age = rs.getInt("age");
  19. String address = rs.getString("address");
  20. float salary = rs.getFloat("salary");
  21. System.out.println( "ID = " + id );
  22. System.out.println( "NAME = " + name );
  23. System.out.println( "AGE = " + age );
  24. System.out.println( "ADDRESS = " + address );
  25. System.out.println( "SALARY = " + salary );
  26. System.out.println();
  27. }
  28. rs.close();
  29. stmt.close();
  30. c.close();
  31. } catch ( Exception e ) {
  32. System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  33. System.exit(0);
  34. }
  35. System.out.println("Operation done successfully");
  36. }
  37. }

上述程序编译和执行时,它会产生以下结果:

  1. Opened database successfully
  2. ID = 1
  3. NAME = Paul
  4. AGE = 32
  5. ADDRESS = California
  6. SALARY = 20000.0
  7. ID = 2
  8. NAME = Allen
  9. AGE = 25
  10. ADDRESS = Texas
  11. SALARY = 15000.0
  12. ID = 3
  13. NAME = Teddy
  14. AGE = 23
  15. ADDRESS = Norway
  16. SALARY = 20000.0
  17. ID = 4
  18. NAME = Mark
  19. AGE = 25
  20. ADDRESS = Rich-Mond
  21. SALARY = 65000.0
  22. Operation done successfully

UPDATE 操作

下面的 Java 代码显示了如何使用 UPDATE 语句来更新任何记录,然后从 COMPANY 表中获取并显示更新的记录:

  1. import java.sql.*;
  2. public class SQLiteJDBC
  3. {
  4. public static void main( String args[] )
  5. {
  6. Connection c = null;
  7. Statement stmt = null;
  8. try {
  9. Class.forName("org.sqlite.JDBC");
  10. c = DriverManager.getConnection("jdbc:sqlite:test.db");
  11. c.setAutoCommit(false);
  12. System.out.println("Opened database successfully");
  13. stmt = c.createStatement();
  14. String sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1;";
  15. stmt.executeUpdate(sql);
  16. c.commit();
  17. ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
  18. while ( rs.next() ) {
  19. int id = rs.getInt("id");
  20. String name = rs.getString("name");
  21. int age = rs.getInt("age");
  22. String address = rs.getString("address");
  23. float salary = rs.getFloat("salary");
  24. System.out.println( "ID = " + id );
  25. System.out.println( "NAME = " + name );
  26. System.out.println( "AGE = " + age );
  27. System.out.println( "ADDRESS = " + address );
  28. System.out.println( "SALARY = " + salary );
  29. System.out.println();
  30. }
  31. rs.close();
  32. stmt.close();
  33. c.close();
  34. } catch ( Exception e ) {
  35. System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  36. System.exit(0);
  37. }
  38. System.out.println("Operation done successfully");
  39. }
  40. }

上述程序编译和执行时,它会产生以下结果:

  1. Opened database successfully
  2. ID = 1
  3. NAME = Paul
  4. AGE = 32
  5. ADDRESS = California
  6. SALARY = 25000.0
  7. ID = 2
  8. NAME = Allen
  9. AGE = 25
  10. ADDRESS = Texas
  11. SALARY = 15000.0
  12. ID = 3
  13. NAME = Teddy
  14. AGE = 23
  15. ADDRESS = Norway
  16. SALARY = 20000.0
  17. ID = 4
  18. NAME = Mark
  19. AGE = 25
  20. ADDRESS = Rich-Mond
  21. SALARY = 65000.0
  22. Operation done successfully

DELETE 操作

下面的 Java 代码显示了如何使用 DELETE 语句删除任何记录,然后从 COMPANY 表中获取并显示剩余的记录:

  1. import java.sql.*;
  2. public class SQLiteJDBC
  3. {
  4. public static void main( String args[] )
  5. {
  6. Connection c = null;
  7. Statement stmt = null;
  8. try {
  9. Class.forName("org.sqlite.JDBC");
  10. c = DriverManager.getConnection("jdbc:sqlite:test.db");
  11. c.setAutoCommit(false);
  12. System.out.println("Opened database successfully");
  13. stmt = c.createStatement();
  14. String sql = "DELETE from COMPANY where ID=2;";
  15. stmt.executeUpdate(sql);
  16. c.commit();
  17. ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
  18. while ( rs.next() ) {
  19. int id = rs.getInt("id");
  20. String name = rs.getString("name");
  21. int age = rs.getInt("age");
  22. String address = rs.getString("address");
  23. float salary = rs.getFloat("salary");
  24. System.out.println( "ID = " + id );
  25. System.out.println( "NAME = " + name );
  26. System.out.println( "AGE = " + age );
  27. System.out.println( "ADDRESS = " + address );
  28. System.out.println( "SALARY = " + salary );
  29. System.out.println();
  30. }
  31. rs.close();
  32. stmt.close();
  33. c.close();
  34. } catch ( Exception e ) {
  35. System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  36. System.exit(0);
  37. }
  38. System.out.println("Operation done successfully");
  39. }
  40. }

上述程序编译和执行时,它会产生以下结果:

  1. Opened database successfully
  2. ID = 1
  3. NAME = Paul
  4. AGE = 32
  5. ADDRESS = California
  6. SALARY = 25000.0
  7. ID = 3
  8. NAME = Teddy
  9. AGE = 23
  10. ADDRESS = Norway
  11. SALARY = 20000.0
  12. ID = 4
  13. NAME = Mark
  14. AGE = 25
  15. ADDRESS = Rich-Mond
  16. SALARY = 65000.0
  17. Operation done successfully