上一篇看完了update,接下来继续看insert,按照正常的理解,其实insert操作用update的那一套应该就可以了。为什么要额外提供一套API呢?跟update一样,找到核心的private-insert方法。
private <T> T insert(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
if (conn == null){throw new SQLException("Null connection");}
if (sql == null){
if (closeConn) {close(conn);}
throw new SQLException("Null SQL statement");
}
if (rsh == null){
if (closeConn) {close(conn);}
throw new SQLException("Null ResultSetHandler");
}
PreparedStatement stmt = null;
T generatedKeys = null;
try{
stmt = this.prepareStatement(conn, sql);
this.fillStatement(stmt, params);
stmt.executeUpdate();
ResultSet resultSet = stmt.getGeneratedKeys();
generatedKeys = rsh.handle(resultSet);
}catch (SQL