java新增之后返回新增列的值 我需要返回自增的id
publicintInsert(Historyh){Connectionconn=null;PreparedStatementstmt=null;try{Class.fo...
public int Insert(History h){
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databasename = CloudDatabase",
"sa",
"123456");
String sql = "insert into History(U_id,G_id,ExamName,Start,TestType,Score,TitleCount) values(?,?,?,getDate(),?,?,?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, h.getU_id());
stmt.setInt(2, h.getG_id());
stmt.setString(3, h.getExamName());
stmt.setInt(4, h.getTestType());
stmt.setInt(5, h.getScore());
stmt.setInt(6, h.getTitleCount());
return stmt.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}finally{
try {
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
} 展开
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databasename = CloudDatabase",
"sa",
"123456");
String sql = "insert into History(U_id,G_id,ExamName,Start,TestType,Score,TitleCount) values(?,?,?,getDate(),?,?,?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, h.getU_id());
stmt.setInt(2, h.getG_id());
stmt.setString(3, h.getExamName());
stmt.setInt(4, h.getTestType());
stmt.setInt(5, h.getScore());
stmt.setInt(6, h.getTitleCount());
return stmt.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}finally{
try {
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
} 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询