Mysql 如何 得到新插入的字段ID
Mysql 如何 得到新插入的字段ID
N年前的代码应该还能用
/** * @param dataSource * @param sql * @param params * @return * @throws SQLException */ public static int insertAndGetId(DataSource dataSource, String sql, Object[] params) throws SQLException { Connection con = dataSource.getConnection(); PreparedStatement pstmt = con.prepareStatement(sql); try { for (int i = 1; i <= params.length; i++) { pstmt.setObject(i, params[i - 1]); } pstmt.executeUpdate(); ResultSet rs = pstmt.executeQuery("SELECT LAST_INSERT_ID()"); rs.next(); int newId = rs.getInt(1); rs.close(); return newId; } finally { try { pstmt.close(); } finally { con.close(); } } }
相关推荐
CoderToy 2020-11-16
emmm00 2020-11-17
王艺强 2020-11-17
ribavnu 2020-11-16
bianruifeng 2020-11-16
wangshuangbao 2020-11-13
苏康申 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
aydh 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
vitasfly 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11
世樹 2020-11-11