I want to INSERT a record in a database (which is Microsoft SQL Server in my case) using JDBC in Java. At the same time, I want to obtain the insert ID. How can
INSERT
You can use following java code to get new inserted id.
ps = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); ps.setInt(1, quizid); ps.setInt(2, userid); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { lastInsertId = rs.getInt(1); }