JDBC驱动下载地址:
http://www.sqlite.com.cn/Upfiles/source/sqlitejdbc-v033-nested.tgz
直接上代码:
CRUDDemo .class
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CRUDDemo {
public Statement connect(String address){//创建连接
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//创建应用程序到数据库的连接通道
Connection connection = null;
try {
connection = DriverManager.getConnection(address);
} catch (SQLException e) {
e.printStackTrace();
}
//创建对数据进行操作的对象
Statement state = null;
try {
state = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return state;
}
// str为相应操作的字符串eg:("create table t_worker(id integer,name text,age integer)
public void createTable(Statement state, String str){//创建表
try {
state.execute(str);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void dropTable(Statement state, String str){//删除表格
try {
state.execute(str);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void insertInfo(Statement state, String str){//插入
try {
state.executeUpdate(str);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateInfo(Statement state, String str){//更新
try {
state.executeUpdate(str);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteInfo(Statement state, String str){//删除表内信息
try {
state.executeUpdate(str);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void selectInfo(Statement state, String str){//查询表内信息
try {
ResultSet rs = state.executeQuery(str);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id+"--"+name);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
测试类:
Test.class
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
CRUD_Sqlite crud = new CRUD_Sqlite();
Test test = new Test();
test.method(crud);
}
public void method(CRUD_Sqlite crud) {
// 定位数据库位置的字符串:如果出现SQLException(out of memory ) 就检查路径是否正确,盘符最好用小写字母
String address = "jdbc:sqlite://d:/sqllite/stu.db";
Statement state = crud.connect(address);
// if not exists 判断相应的表是否不存在,若表存在则不创建表。id为 int、主键 、自增
String createTable = "create table if not exists t_stu_info (id integer primary key autoincrement ,name text)";
// if exists 判断相应的表是否存在,若表存在则删除表
String dropTable = "drop table if exists t_stu_info";
// 删除表
crud.dropTable(state, dropTable);
// 创建表
crud.createTable(state, createTable);
// 向表内插入信息
String insert = "insert into t_stu_info values (1,'星仔') ";
crud.insertInfo(state, insert);
insert = "insert into t_stu_info values (2,'猴子') ";
crud.insertInfo(state, insert);
insert = "insert into t_stu_info values (3,'疯子') ";
crud.insertInfo(state, insert);
insert = "insert into t_stu_info values (4,'叶子') ";
crud.insertInfo(state, insert);
insert = "insert into t_stu_info values (5,'虫子') ";
crud.insertInfo(state, insert);
// 更新id为1的name字段
String updateInfo = "update t_stu_info set name = '小伟子' where id = 1";
crud.updateInfo(state, updateInfo);
// 删除id为1的行
String deleteInfo = "delete from t_stu_info where id = 1";
crud.deleteInfo(state, deleteInfo);
// 查询表内全部内容
String select = "select * from t_stu_info ";
crud.selectInfo(state, select);
}
}