一. 插入(采用变量的方式插入)
1.创建数据源.DateSource
2.建立连接
3.构造SQL语句
4.执行SQL语句
5.释放资源
public class TestSQLite {public static void main(String[] args) throws SQLException {textInsert();}public static void textInsert() throws SQLException {//插入操作//1.创建数据源.DataSourceDataSource dataSource = new SQLiteDataSource();((SQLiteDataSource) dataSource).setUrl("jdbc:sqlite://D:/App/sqlite-tools-win-x64-3450100/text.db");//2.建立连接Connection connection = dataSource.getConnection();//3.构造SQL语句String sql = "insert into text values(?,?)";PreparedStatement Statement = connection.prepareStatement(sql);Statement.setInt(1,11);Statement.setString(2,"张三");//4.执行SQL语句Statement.executeUpdate();//5.释放资源 根据反顺序释放资源Statement.close();connection.close();}
写好代码后,我们需要点击运行,代码左边的绿色三角,运行结束后,去sqlite3.exe
所在的文件夹中,shift+右键,进入shell控制台,查询增加结果
PS D:\App\sqlite-tools-win-x64-3450100> .\sqlite3.exe text.db
SQLite version 3.45.1 2024-01-30 16:01:20 (UTF-16 console I/O)
Enter ".help" for usage hints.
sqlite> select * from text;
1|黄橙子
2|沈月本人
3|驼女士
11|张三
sqlite>
二.查询
从sqlite
中进行查询
1.创建数据源.DateSource
2.建立连接
3.构造SQL语句
4.执行SQL语句
5.遍历结果集合
6.释放资源
public static void textSelect() throws SQLException {//查询操作//从sqlite中进行查询//1.创建数据源.DateSourceDataSource dataSource = new SQLiteDataSource();((SQLiteDataSource)dataSource).setUrl("jdbc:sqlite://D:/App/sqlite-tools-win-x64-3450100/text.db");//描述数据源 前缀一定要写好jdbc:sqlite://,后面再跟上该数据库所在的地址//2.建立连接Connection connection = dataSource.getConnection();//3.构造SQL语句String sql = "select * from text";PreparedStatement statement = connection.prepareStatement(sql);//4.执行SQL语句ResultSet resultSet = statement.executeQuery();//5.遍历结果集合while(resultSet.next()){int id = resultSet.getInt("id");String name = resultSet.getString("name");System.out.println("id:"+id+",name:"+name);}//6.释放资源resultSet.close();statement.close();connection.close();}
}
运行结果