使用JDBC获取多个结果集并不起作用

时间:2017-06-13 08:42:16

标签: java sql-server hibernate jdbc

我应该从第三方SQL Server数据库调用存储过程(拥有只读的权限)。 此外,当我尝试在DataGrip中执行此过程时:

import matplotlib.pyplot as plt x=[1,2,3] y=[1,2,3] typ=["big","small","big"] color = {"big" : "indigo", "small" : "mediumvioletred"} p1 = plt.bar(x,y, color=[color[t] for t in typ]) handles = [plt.Rectangle((0,0),1,1, color=color[c]) for c in set(typ)] plt.legend(handles=handles, labels=set(typ)) plt.show()

我收到了两个结果:

冷杉:

EXEC Web.example_procedure 2, 3, 4

第二:

<anonymous>
-----------
3

我需要第二张桌子。

现在由于article

,我正在做下一步
column_1 | column_2
------------------
   k1    |   v1
   k2    |   v2
   k3    |   v3
...

输出是:

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(); } }

换句话说,我只得到第一个结果。 如何获得第二张表?

  • 我可以修改SQL查询吗? (这将只返回一个没有的表 第一个结果)
  • JDBC?
  • 休眠?

我会很高兴任何工作变体。

更新

感谢@MarkRotteveel和他的answer - 我解决了问题

RESULT SET #1

2 个答案:

答案 0 :(得分:1)

使用JDBC CallableStatement:

  

cstmt.registerOutParameter()
  cstmt.getObject()

  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);

https://docs.oracle.com/cd/E17952_01/connector-j-en/connector-j-usagenotes-statements-callable.html

答案 1 :(得分:0)

您可以将Resultset设置为可更新以执行多个命令。

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();