java通过jdbc连接sqlite数据库进行CRUD操作

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);
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值