针对您的问题,“详解Java使用sqlite 数据库如何生成db文件”,我可以提供以下步骤指导:
准备工作
- 安装SQLite数据库:下载地址为:https://www.sqlite.org/download.html。
- 下载Java SQLite JDBC 驱动程序:下载地址为:https://bitbucket.org/xerial/sqlite-jdbc/downloads/。
- 创建Java项目。
创建数据表
- 在Java项目中创建一个名为“myDB.db”的SQLite数据库文件。
- 在Java代码中引入SQLite-JDBC驱动程序。
Class.forName("org.sqlite.JDBC");
- 通过以下代码连接到SQLite数据库:
Connection conn = DriverManager.getConnection("jdbc:sqlite:/path/to/myDB.db");
其中,/path/to/表示数据库文件的路径。如果数据库文件在项目的根目录下,可以使用以下路径:
Connection conn = DriverManager.getConnection("jdbc:sqlite:myDB.db");
- 创建数据表。例如,创建一个存储用户信息的数据表:
String sql = "CREATE TABLE users (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT," +
"email TEXT," +
"age INTEGER" +
");";
Statement stmt = conn.createStatement();
stmt.execute(sql);
这将在myDB.db数据库中创建一个名为“users”的表。
插入数据
- 在Java中插入数据需要使用一个PreparedStatement对象。例如:
String sql = "INSERT INTO users (name, email, age) VALUES (?, ?, ?);";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "张三");
pstmt.setString(2, "zhangsan@example.com");
pstmt.setInt(3, 25);
pstmt.executeUpdate();
这将在“users”表中插入一条数据.
查询数据
- 查询表中所有数据:
String sql = "SELECT * FROM users;";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getInt("id") + "\t" +
rs.getString("name") + "\t" +
rs.getString("email") + "\t" +
rs.getInt("age"));
}
- 查询表中符合条件的数据:
String sql = "SELECT * FROM users WHERE age > ?;";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 18);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
System.out.println(rs.getInt("id") + "\t" +
rs.getString("name") + "\t" +
rs.getString("email") + "\t" +
rs.getInt("age"));
}
这将查询“users”表中年龄大于18岁的所有数据。
以上是创建SQLite数据库并使用Java对数据库进行增删改查的完整攻略。
下面是两条完整的示例,用于演示如何生成db文件:
示例1:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class SQLiteDemo {
public static void main(String[] args) {
Connection conn = null;
try {
//连接到SQLite数据库
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:test.db");
System.out.println("连接到SQLite数据库成功");
//创建表
Statement stmt = conn.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);
//插入数据
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO COMPANY(ID,NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?,?)");
pstmt.setInt(1, 1);
pstmt.setString(2, "张三");
pstmt.setInt(3, 20);
pstmt.setString(4, "北京市");
pstmt.setDouble(5, 8000.0);
pstmt.executeUpdate();
pstmt = conn.prepareStatement("INSERT INTO COMPANY(ID,NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?,?)");
pstmt.setInt(1, 2);
pstmt.setString(2, "李四");
pstmt.setInt(3, 25);
pstmt.setString(4, "上海市");
pstmt.setDouble(5, 15000.0);
pstmt.executeUpdate();
//查询数据
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM COMPANY");
while (rs.next()) {
System.out.println(rs.getInt("ID") + "\t" +
rs.getString("NAME") + "\t" +
rs.getInt("AGE") + "\t" +
rs.getString("ADDRESS") + "\t" +
rs.getDouble("SALARY"));
}
//关闭连接
rs.close();
pstmt.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例2:
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class GenerateDBFile {
public static void main(String[] args) {
File file = new File("mydb.db"); //指定数据库文件路径
Connection conn = null;
try {
if (file.exists()) {
file.delete();
}
//连接到SQLite数据库
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:mydb.db");
System.out.println("连接到SQLite数据库成功");
//创建表
Statement stmt = conn.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);
//插入数据
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO COMPANY(ID,NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?,?)");
pstmt.setInt(1, 1);
pstmt.setString(2, "张三");
pstmt.setInt(3, 20);
pstmt.setString(4, "北京市");
pstmt.setDouble(5, 8000.0);
pstmt.executeUpdate();
pstmt = conn.prepareStatement("INSERT INTO COMPANY(ID,NAME,AGE,ADDRESS,SALARY) VALUES (?,?,?,?,?)");
pstmt.setInt(1, 2);
pstmt.setString(2, "李四");
pstmt.setInt(3, 25);
pstmt.setString(4, "上海市");
pstmt.setDouble(5, 15000.0);
pstmt.executeUpdate();
//查询数据
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM COMPANY");
while (rs.next()) {
System.out.println(rs.getInt("ID") + "\t" +
rs.getString("NAME") + "\t" +
rs.getInt("AGE") + "\t" +
rs.getString("ADDRESS") + "\t" +
rs.getDouble("SALARY"));
}
//关闭连接
rs.close();
pstmt.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上是如何生成db文件的两条完整示例,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解Java使用sqlite 数据库如何生成db文件 - Python技术站