📅  最后修改于: 2020-11-13 01:04:00             🧑  作者: Mango
要将SQLite与Java程序一起使用,必须在系统上设置SQLite JDBC驱动程序和Java。请按照以下步骤操作:
使用以下代码使用Java编程语言连接到SQLite数据库:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
/**
* Connect to a sample database
*/
public static void connect() {
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:C:/sqlite/JTP.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();
}
}
它与您已经创建的数据库JTP.db连接。
您也可以使用Java编程语言在SQLite中创建新数据库。让我们创建一个名为“ SSSIT.db”的数据库。创建一个公共类“ Create”,并使用以下代码:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Create {
public static void createNewDatabase(String fileName) {
String url = "jdbc:sqlite:C:/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("SSSIT.db");
}
}
现在创建一个名为“ SSSIT.db”的新数据库。您可以在安装了sqlite的位置看到它。
让我们创建一个名为“ employees”的表,其中包含“ name”和“ capacity”列。创建具有以下代码的类名称“ CreateTable”:
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:C://sqlite/SSSIT.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);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
createNewTable();
}
}
它将在SSSIT.db数据库中创建一个表“ employees”。
创建表后,使用以下代码在表中插入一些记录。创建一个具有以下代码的新类“ InsertRecords”:
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:C://sqlite/SSSIT.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("Aryan", 30000);
app.insert("Robert", 40000);
app.insert("Jerry", 50000);
}
}
现在,记录已插入表中。您可以使用SELECT命令将其检出:
要从表中选择记录,请使用以下代码。创建一个新类“ SelectRecords”,其中包含以下数据。
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:C://sqlite/SSSIT.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();
}
}
输出:
您会看到它显示了我们插入一次的所有记录。
注意:通过使用相同的过程,您可以更新和删除表和数据库。