📅  最后修改于: 2023-12-03 14:47:39.814000             🧑  作者: Mango
SQLite-Java是Java语言中一个轻量级的关系型数据库,它采用的是磁盘驻留的、无服务器的方式,所以不需要一个单独的大型的数据库服务器进程。SQLite是在跨平台软件领域广泛应用的数据库,它可以在多种操作系统中使用。
在Java项目中使用SQLite-Java需要先下载SQLite-Java的jar包,可以直接从sqlite-jdbc官网下载最新版本的jar包,然后将其导入到项目中即可。
创建SQLite-Java数据库很简单,只需要导入相应的jar包,就可以使用java.sql
包中的类来创建和管理数据库。
import java.sql.*;
public class CreateDatabase {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:test.db");
System.out.println("创建数据库成功!");
} catch ( ClassNotFoundException | SQLException e ) {
System.err.println(e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
}
以上代码演示了如何创建一个名为test.db
的SQLite-Java数据库,当然在创建成功之前需要确认是否已经在本地安装SQLite或者SQLite-Java的依赖包。
连接SQLite-Java数据库是最初需要做的一件事情,创建连接对象的时候,需要指定连接字符串。在SQLite中,连接字符串只是一个文件路径,这个文件就是SQLite数据库文件。
下面是一个连接SQLite-Java数据库的示例:
import java.sql.*;
public class DBConnection {
private Connection connection;
public Connection getConnection(String path) {
try {
Class.forName("org.sqlite.JDBC");
String url = "jdbc:sqlite:" + path;
connection = DriverManager.getConnection(url);
System.out.println("数据库连接成功!");
}
catch (SQLException | ClassNotFoundException e) {
System.out.println(e.getMessage());
}
return connection;
}
}
在以上代码中,getConnection方法接收数据库的文件路径作为参数,并返回连接对象connection。我们可以在程序中通过连接对象进行相关的操作,比如增、删、改、查等。
在SQLite-Java中,我们可以使用java.sql
包中的Statement和PreparedStatement来对数据库进行增、删、改、查等操作。
import java.sql.*;
public class CRUD {
private Connection connection;
public CRUD(Connection connection) {
this.connection = connection;
}
public void createTable() {
try {
String sql = "CREATE TABLE COMPANY " +
"(ID INT PRIMARY KEY NOT NULL," +
" NAME TEXT NOT NULL, " +
" AGE INT NOT NULL, " +
" ADDRESS CHAR(50), " +
" SALARY REAL)";
Statement statement = connection.createStatement();
statement.executeUpdate(sql);
statement.close();
System.out.println("创建表成功!");
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public void insertRecord() {
try {
connection.setAutoCommit(false);
String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
"VALUES (1, 'Paul', 32, 'California', 20000.00)";
Statement statement = connection.createStatement();
statement.executeUpdate(sql);
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
"VALUES (2, 'Allen', 25, 'Texas', 15000.00)";
statement.executeUpdate(sql);
connection.commit();
statement.close();
System.out.println("插入记录成功!");
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
public void queryData() {
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM COMPANY");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String address = resultSet.getString("address");
float salary = resultSet.getFloat("salary");
System.out.println("ID = " + id
+ ", NAME = " + name
+ ", AGE = " + age
+ ", ADDRESS = " + address
+ ", SALARY = " + salary);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
public void updateData() {
try {
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
String sql = "UPDATE COMPANY set SALARY = 25000.00 where ID = 1;";
statement.executeUpdate(sql);
connection.commit();
ResultSet resultSet = statement.executeQuery("SELECT * FROM COMPANY");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String address = resultSet.getString("address");
float salary = resultSet.getFloat("salary");
System.out.println("ID = " + id
+ ", NAME = " + name
+ ", AGE = " + age
+ ", ADDRESS = " + address
+ ", SALARY = " + salary);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
public void deleteData() {
try {
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
String sql = "DELETE from COMPANY where ID = 2;";
statement.executeUpdate(sql);
connection.commit();
System.out.println("删除记录成功!");
ResultSet resultSet = statement.executeQuery("SELECT * FROM COMPANY");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String address = resultSet.getString("address");
float salary = resultSet.getFloat("salary");
System.out.println("ID = " + id
+ ", NAME = " + name
+ ", AGE = " + age
+ ", ADDRESS = " + address
+ ", SALARY = " + salary);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
}
在以上代码中,我们定义了一个包含增、删、改、查等操作的CRUD类,并通过数据库连接对象实例化该类,使用Statement对象来执行SQL语句。
以上就是对SQLite-Java的简单介绍,SQLite-Java是一款轻量级的数据库,易于管理和使用,其语法简单易懂,并且性能优秀。SQLite-Java支持事务操作,并且能够在多个平台上运行,可以满足小型Java项目的数据库需求。