【问题标题】:Connection from Java Application and Stored Procedure MSSQL来自 Java 应用程序和存储过程 MSSQL 的连接
【发布时间】:2013-09-26 09:36:39
【问题描述】:

我在 SQL Sever 中有存储过程,它有一些参数。我想从组合框中(在 java 应用程序中)给出参数的值。我已经阅读了这段代码(请看下面)

public static void executeSprocInParams(Connection con) {
   try {
      PreparedStatement pstmt = con.prepareStatement("{call dbo.uspGetEmployeeManagers(?)}");
      pstmt.setInt(1, 50);
      ResultSet rs = pstmt.executeQuery();

      while (rs.next()) {
         System.out.println("EMPLOYEE:");
         System.out.println(rs.getString("LastName") + ", " + rs.getString("FirstName"));
         System.out.println("MANAGER:");
         System.out.println(rs.getString("ManagerLastName") + ", " + rs.getString("ManagerFirstName"));
         System.out.println();
      }
      rs.close();
      pstmt.close();
   }

   catch (Exception e) {
      e.printStackTrace();
    }
}

但我没明白意思。有没有任何教程可以给我一些例子,就像我的情况一样?感谢您的任何回复

【问题讨论】:

    标签: java sql-server-2008 stored-procedures parameters


    【解决方案1】:
    PreparedStatement pstmt = con.prepareStatement("{call dbo.uspGetEmployeeManagers(?)}");
      pstmt.setInt(1, 50);
      ResultSet rs = pstmt.executeQuery();
    

    1) 第 1 行使用您的存储过程创建一个准备语句对象。这 ?是存储过程的输入参数的占位符 2) 第 2 行将输入参数设置为存储过程 3) executeQuery 通过提供输入来执行存储过程,并将输出作为结果集。

    while (rs.next()) {
         System.out.println("EMPLOYEE:");
         System.out.println(rs.getString("LastName") + ", " + rs.getString("FirstName"));
         System.out.println("MANAGER:");
         System.out.println(rs.getString("ManagerLastName") + ", " + rs.getString("ManagerFirstName"));
         System.out.println();
      }
      rs.close();
      pstmt.close();
    

    以上行遍历结果集并打印每条记录

    【讨论】:

      【解决方案2】:
      public static void executeSprocInParams(Connection con) {
         try {
            PreparedStatement pstmt = con.prepareStatement("{call dbo.uspGetEmployeeManagers(?)}");//Creating a prepared statement with the string to execute your procedure.
            pstmt.setInt(1, 50);//This is to set the parameter to the place holder '?'
            ResultSet rs = pstmt.executeQuery();//This is to execute your procedure and put the result into a table like set
      
        while (rs.next()) {//To check if there are any values in the set, if so the print those values
           System.out.println("EMPLOYEE:");
           System.out.println(rs.getString("LastName") + ", " + rs.getString("FirstName"));
           System.out.println("MANAGER:");
           System.out.println(rs.getString("ManagerLastName") + ", " + rs.getString("ManagerFirstName"));
           System.out.println();
        }
        rs.close();//close the set
        pstmt.close();//close the statement
         }
      
         catch (Exception e) {
            e.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多