Java 使用JDBC&x27获取多个结果集;行不通
我应该从第三方SQL Server数据库调用存储过程(具有只读权限)。 另外,当我尝试在DataGrip中执行此过程时:Java 使用JDBC&x27获取多个结果集;行不通,java,sql-server,hibernate,jdbc,Java,Sql Server,Hibernate,Jdbc,我应该从第三方SQL Server数据库调用存储过程(具有只读权限)。 另外,当我尝试在DataGrip中执行此过程时: EXEC Web.example_程序2、3、4 我收到了两个结果: 冷杉: 我需要第二张桌子 由于这个原因,我现在正在做下一个 输出为: 结果集#1 换句话说,我只得到第一个结果。 如何得到第二张桌子 我可以修改SQL查询吗?(它将只返回一个表,而不返回 第一个整数(结果) JDBC 冬眠 我很乐意为您提供任何工作变体 更新 多亏了@MarkRotteveel和他的-我
EXEC Web.example_程序2、3、4
我收到了两个结果:
冷杉:
我需要第二张桌子
由于这个原因,我现在正在做下一个
输出为:
结果集#1
换句话说,我只得到第一个结果。
如何得到第二张桌子
- 我可以修改SQL查询吗?(它将只返回一个表,而不返回 第一个整数(结果)
- JDBC
- 冬眠
String sql = "EXEC Web.example_procedure 2, 3, 4";
PreparedStatement stmt = con.prepareStatement(sql);
boolean result = stmt.execute();
while (true) {
if (result) {
ResultSet rs = stmt.getResultSet();
// in my case first table has only one column,
// and I need the second table, which has 9 columns
if (rs.getMetaData().getColumnCount() > 1) {
// go through the rows
while (rs.next()) {
// for example what we can do
rs.getMetaData().getColumnCount(); // return column count in the current result set
rs.getObject(int columnIndex); // get value for column index. Must be not greater than .getColumnCount()
}
}
} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1) {
// no more results
break;
}
}
result = stmt.getMoreResults();
}
使用JDBC CallableStatement: cstmt.registerOutParameter()
cstmt.getObject()
您可以将结果集设置为可更新以执行多个命令
Statement stmt = conn1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String insert1="insert into data values('******','*********')";
String insert2="insert into data values('*******','******')";
conn1.setAutoCommit(false);
ResultSet rs = stmt.executeQuery("select * from data");
rs.last();
您没有显示存储过程的代码;您可能需要考虑是否存在与结果集交错的更新计数(例如,如果SP中没有
设置nocount on
)<代码>结果为假
并不意味着没有结果,它意味着当前结果是更新计数。看看@MarkRotterVeel,实际上我没有SP代码。但是现在我会要求接收它。无论如何,试着按照我在链接答案中描述的那样处理结果,它可能会解决您的问题。@MarkRotteveel非常感谢您!这是真正的帮助。如果您使用jTDS驱动器而不是SQLServer的Microsoft驱动程序,通常会观察到这种行为。在这种情况下,只要updateCount!=-1和结果!=谢谢你的建议!已尝试,但仍只能访问第一个结果。
private static void executeStatement(Connection con) {
try {
String SQL = "EXEC Web.example_procedure 2, 3, 4";
Statement stmt = con.createStatement();
boolean results = stmt.execute(SQL);
int rsCount = 0;
//Loop through the available result sets.
do {
if (results) {
ResultSet rs = stmt.getResultSet();
rsCount++;
//Show data from the result set.
System.out.println("RESULT SET #" + rsCount);
while (rs.next()) {
// something will be here
}
rs.close();
}
results = stmt.getMoreResults();
} while (results);
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String sql = "EXEC Web.example_procedure 2, 3, 4";
PreparedStatement stmt = con.prepareStatement(sql);
boolean result = stmt.execute();
while (true) {
if (result) {
ResultSet rs = stmt.getResultSet();
// in my case first table has only one column,
// and I need the second table, which has 9 columns
if (rs.getMetaData().getColumnCount() > 1) {
// go through the rows
while (rs.next()) {
// for example what we can do
rs.getMetaData().getColumnCount(); // return column count in the current result set
rs.getObject(int columnIndex); // get value for column index. Must be not greater than .getColumnCount()
}
}
} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1) {
// no more results
break;
}
}
result = stmt.getMoreResults();
}
String sql = "{call getEmpName (?, ?)}";
cstmt = conn.prepareCall(sql);
//Bind IN parameter first, then bind OUT parameter
int empID = 102;
cstmt.setInt(1, empID); // This would set ID as 102
// Because second parameter is OUT so register it
cstmt.registerOutParameter(2, OracleTypes.CURSOR);
//Use execute method to run stored procedure.
System.out.println("Executing stored procedure..." );
cstmt.execute();
//Retrieve data
rs = (ResultSet) cstmt.getObject(1);
Statement stmt = conn1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String insert1="insert into data values('******','*********')";
String insert2="insert into data values('*******','******')";
conn1.setAutoCommit(false);
ResultSet rs = stmt.executeQuery("select * from data");
rs.last();