要使用java程序连接SQLite,并与SQLite进行数据存取操作,必须在系统上设置SQLite JDBC驱动程序和安装Java JDK。按照以下步骤进行:
sqlite-jdbc-(VERSION).jar
。打开Eclipse IDE,创建一个JAVA工程:JavaWithSQLite,下载所需的sqlite-jdbc-(VERSION).jar
(下载地址:http://bitbucket.org/xerial/sqlite-jdbc/downloads/ 或者 http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.18.0 ),并将它放入到项目的类库中。
使用以下代码使用Java编程语言连接到SQLite数据库,首先创建一个类:ConnectSQLite.java
,其代码如下所示 -
package com.zyiz; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectSQLite { /** * Connect to a sample database */ public static void connect() { Connection conn = null; try { // db parameters String url = "jdbc:sqlite:D:/software/sqlite/java-sqlite.db"; // create a connection to the database conn = DriverManager.getConnection(url); System.out.println("Connection to SQLite has been established."); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } } } /** * @param args * the command line arguments */ public static void main(String[] args) { connect(); } }
执行上面代码后,应该会创建一个文件:D:/software/sqlite/java-sqlite.db
,并与数据库java-sqlite.db
连接。
还可以使用java编程语言在SQLite中创建一个新的数据库。假设要使用Java来创建一个名为java_sqlite.db的数据库。创建一个公共类:CreateDB.java
并使用以下代码:
package com.zyiz; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; public class CreateDB { public static void createNewDatabase(String fileName) { String url = "jdbc:sqlite:" + fileName; try { Connection conn = DriverManager.getConnection(url); if (conn != null) { DatabaseMetaData meta = conn.getMetaData(); System.out.println("The driver name is " + meta.getDriverName()); System.out.println("A new database has been created."); } } catch (SQLException e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { createNewDatabase("D:/software/sqlite/create-db.db"); } }
执行上面代码,得到以下结果 -
The driver name is SQLiteJDBC A new database has been created.
现在创建一个名为“create-db.db
”的新数据库。可以看到对应创建目录有一个文件:create-db.db
存在。
假设要通过Java程序在SQLite中创建一个名为tb_emp
的表,tb_emp
表具有id
,name
和capacity
这三列。首先创建一个Java类:CreateTable.java
,并使用以下代码:
package com.zyiz; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class CreateTable { public static void createNewTable() { // SQLite connection string String url = "jdbc:sqlite:D:/software/sqlite/java-sqlite.db"; // SQL statement for creating a new table String sql = "CREATE TABLE IF NOT EXISTS employees (\n" + " id integer PRIMARY KEY,\n" + " name text NOT NULL,\n" + " capacity real\n" + ");"; try { Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement(); stmt.execute(sql); System.out.println("Create table finished."); } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * @param args * the command line arguments */ public static void main(String[] args) { createNewTable(); } }
执行上面代码,得到以下结果 -
Create table finished.
执行上面代码后,将在java-sqlite.db
数据库中创建一个名称为:employees
的表。
创建表后,使用以下代码在表中插入一些记录。 创建一个新的Java类:InsertRecords
,具有以下代码:
package com.zyiz; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class InsertRecords { private Connection connect() { // SQLite connection string String url = "jdbc:sqlite:D:/software/sqlite/java-sqlite.db"; Connection conn = null; try { conn = DriverManager.getConnection(url); } catch (SQLException e) { System.out.println(e.getMessage()); } return conn; } public void insert(String name, double capacity) { String sql = "INSERT INTO employees(name, capacity) VALUES(?,?)"; try { Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setDouble(2, capacity); pstmt.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { InsertRecords app = new InsertRecords(); // insert three new rows app.insert("Maxsu", 30000); app.insert("Minsu", 40000); app.insert("Miswong", 50000); System.out.println("Insert data finished."); } }
现在记录被插入到表中。 可以使用SELECT
命令查看它:
sqlite> .open java-sqlite.db sqlite> .tables employees sqlite> select * from employees; id,name,capacity 1,Maxsu,30000.0 2,Minsu,40000.0 3,Miswong,50000.0 sqlite> sqlite>
要使用Java程序从表中选择/查询记录,请使用以下代码。 创建一个新的Java类 - SelectRecords.java
,使用以下代码 -
package com.zyiz; import java.sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class SelectRecords { private Connection connect() { // SQLite connection string String url = "jdbc:sqlite:D:/software/sqlite/java-sqlite.db"; Connection conn = null; try { conn = DriverManager.getConnection(url); } catch (SQLException e) { System.out.println(e.getMessage()); } return conn; } public void selectAll() { String sql = "SELECT * FROM employees"; try { Connection conn = this.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); // loop through the result set while (rs.next()) { System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getDouble("capacity")); } } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * @param args * the command line arguments */ public static void main(String[] args) { SelectRecords app = new SelectRecords(); app.selectAll(); } }
执行上面代码,得到以下结果 -
注意: 因为都是通过使用相同的编程过程,所以您可自己编写其它数据库操作,比如:更新和删除表中的数据等等。