获取新插入数据的ID

获取新插入数据的ID

  private void insertQus(String question, String choose, String answer,
                String type) { Connection conn= null; PreparedStatement ps= null; try { conn=JdbcUtils. getConnection(); String sql= "insert into questions(question,moption,answer,qusstyle) VALUES(?,?,?,?)"; ps=conn.prepareStatement(sql,Statement. RETURN_GENERATED_KEYS); //在上条语句中添加Statement.RETURN_GENERATED_KEYS参数。 ps.setString(1, question); ps.setString(2, choose); ps.setString(3, answer); ps.setString(4, type); ps.executeUpdate(); //获取KeyID ResultSet rs=ps.getGeneratedKeys(); rs.next(); int num=rs.getInt(1); System. out.println( "自动增长列为:" +num); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtils. close(ps, conn); }
     }

你可能感兴趣的:(获取新插入数据的ID)