JDBC获得数据库自增长值
String sqlStr = "insert into TIMU(ITEM_NAME,ITEM_CONTENT,ITEMTYPE_ID,USER_ID,COURSE_ID," +
"ITEM_KEY,ITEM_SCORE,ITEM_ANSWER) values(" +
"'" + itemName + "'," +
"'" + content + "'," +
typeId + "," +
"'" + userId + "'," +
"'" + courseId + "'," +
"'" + itemKey + "'," +
"'" + score + "'," +
"'" + finalItemAns + "')";
Connection conn = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
int generateId = 0;
try{
conn = ConnCreator.getConnection();
pstmt = conn.prepareStatement(sqlStr, Statement.RETURN_GENERATED_KEYS);
pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
if (rs.next()) {
generateId = rs.getInt(1);
LogHelper.log("新增习题信息成功,sql:" + sqlStr);
} else{
LogHelper.log("新增习题信息失败,sql:" + sqlStr);
}
}catch(Exception ex){
}finally{
try {
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}