获取数据库自动生成的主键:
update 更新操作以后,如果需要用到结果集,可以通过 PreparedStatement.getResultSet()方法获得结果集;
getGeneratedKeys(); 获取执行完插入语句后自动生成的键,结果封装在一个结果集里面;
PreparedStatement 对象,进行预编译的时候,可以接受2个参数,第二个参数表示是自动获得自动生成的键。
获得的结果,封装在一个 结果集 里面。然后调用 PreparedStatement.getGeneratedKeys(); 拿到这个结果集 ;
代码演示:
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
String sql = "INSERT INTO testBlob(image) VALUES(?)";
// 接受2个参数,第二个参数,是否自动获得自动生成的键
Statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
File file = new File("src/ijava/xin/images/th2.jpg");
InputStream inputStream = new FileInputStream(file);