连接sqlServer数据库&jpa调用存储过程Java获取存储过程返回的多个结果集JAVA调用sqlserver存储过程的实现(返回多个结果集的实现)jdbc多结果集(getMoreResults)
存储过程:
- BEGIN
- select * from teacher;
- SELECT * FROM student;
- END
public Object GetMyBOProjectProductList(@PathVariable(value = "userid") int userid) {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://10.110.110.110:1433;DatabaseName=B2BAug";
Connection conn = null;
CallableStatement cs = null;// PreparedStatement,Statement
//ResultSet rs;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, "sa", "1234%abc");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
cs = conn.prepareCall("{call UP_GetMyPuctList(?)}");
cs.setString(1, "" + userid);
boolean flag = cs.execute();
ResultSet rs = cs.getResultSet();
while (rs != null && rs.next()) {
System.out.println("ID:" + rs.getString(1));
}
if (cs.getMoreResults()) {
rs = cs.getResultSet();
while (rs != null && rs.next()) {
System.out.println("ID:" + rs.getString(1));
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (cs != null)
cs.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return userid;
}
参考文档:
http://www.mamicode.com/info-detail-1697679.html
https://blog.csdn.net/ruiguang21/article/details/77890372
https://blog.csdn.net/zmhzhangminhua/article/details/38704281
http://arthur-3726.iteye.com/blog/438700