下载sqlite:
https://www.sqlite.org/download.html
sqlite-dll-win64-x64-3290000
sqlite-tools-win32-x86-3290000
到sqlite-tools-win32-x86-3290000目录下运行sqlite3.exe,创建ems数据库,并建表,插入一条数据,
本地I:\my_program\sqlite-tools-win32-x86-3290000目录下会生成ems.db文件
sqlite> .open ems.db
sqlite> create table xx(
...> id int primary key not null,
...> name text not null,
...> age int not null
...> );
sqlite> .header on
sqlite> .mode column
sqlite> insert into xx values(1,'zgx',22);
sqlite> select * from xx;
id name age
---------- ---------- ----------
1 zgx 22
sqlite>
gradle依赖配置:
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.28.0'
java访问sqlite数据库
package com.codegen.core;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
private static final String driver = "org.sqlite.JDBC";
private static final String url = "jdbc:sqlite:I:/my_program/sqlite-tools-win32-x86-3290000/ems.db";
public static void main(String[] args) {
Test test = new Test();
test.getTableInfo();
}
private Connection getCon() {
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
private void closeAll(Connection con, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void getTableInfo() {
String sql = "select * from xx";
try {
Connection con = this.getCon();
PreparedStatement pmt = con.prepareStatement(sql);
ResultSet rs = pmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("name"));
}
this.closeAll(con, pmt, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
}