下面将对 SQLite 的 t_user 表进行 CRUD 操作,数据表结构如下图:
Java 代码如下:
package com.hxstrive.sqlite; import java.sql.*; import java.util.Date; /** * 对 SQLite 的 T_USER 表进行 CRUD 操作 * @author hxstrive.com 2022/9/29 */ public class SqliteCRUD { public static void main(String[] args) throws Exception { new SqliteCRUD(); } public SqliteCRUD() throws Exception { Connection connection = null; Statement statement = null; try { // 打开数据库 Class.forName("org.sqlite.JDBC"); connection = DriverManager.getConnection("jdbc:sqlite:sqlite_studio.db"); System.out.println("Opened database successfully"); // 插入用户信息 addUser(connection, 1000, "张三", 28, "男", new Date()); // 修改用户信息 updateUser(connection, 1000, "张三-update", 30); // 查询用户信息 getUser(connection, 1000); // 删除用户信息 deleteUser(connection, 1000); } catch (Exception e) { e.printStackTrace(); } finally { if(null != connection) { connection.close(); } } } /** * 添加用户 * @param connection * @param name 用户名称 * @param age 年龄 * @param sex 性别 * @param birthday 生日 */ private void addUser(Connection connection, int id, String name, int age, String sex, Date birthday) throws Exception { PreparedStatement statement = null; try { statement = connection.prepareStatement( "insert into t_user(id, name,age,sex,birthday) values (?,?,?,?,?)"); statement.setInt(1, id); statement.setString(2, name); statement.setInt(3, age); statement.setString(4, sex); statement.setDate(5, new java.sql.Date(birthday.getTime())); statement.execute(); } finally { if(null != statement) { statement.close(); } } } /** * 根据用户ID更新用户信息 * @param id 用户ID * @param newName 用户新名称 */ private void updateUser(Connection connection, int id, String newName, int newAge) throws Exception { PreparedStatement statement = null; try { statement = connection.prepareStatement( "update t_user set name=?,age=? where id=?"); statement.setString(1, newName); statement.setInt(2, newAge); statement.setInt(3, id); statement.executeUpdate(); } finally { if(null != statement) { statement.close(); } } } /** * 根据用户ID获取用户信息 * @param id 用户ID */ private void getUser(Connection connection, int id) throws Exception { PreparedStatement statement = null; ResultSet resultSet = null; try { statement = connection.prepareStatement( "select * from t_user where id=?"); statement.setInt(1, id); resultSet = statement.executeQuery(); if(resultSet.next()) { int vid = resultSet.getInt("id"); String vname = resultSet.getString("name"); int vage = resultSet.getInt("age"); String vsex = resultSet.getString("sex"); java.sql.Date vbirthday = resultSet.getDate("birthday"); System.out.printf("id=%d, name=%s, age=%d, sex=%s, birthday=%s\n", vid, vname, vage, vsex, vbirthday.toString()); } } finally { if(null != resultSet) { resultSet.close(); } if(null != statement) { statement.close(); } } } /** * 根据用户ID删除用户信息 * @param id 用户ID */ private void deleteUser(Connection connection, int id) throws Exception { PreparedStatement statement = null; try { statement = connection.prepareStatement( "delete from t_user where id=?"); statement.setInt(1, id); statement.executeUpdate(); } finally { if(null != statement) { statement.close(); } } } }
运行示例,输出如下:
Opened database successfully id=1000, name=张三-update, age=30, sex=男, birthday=2022-09-29