简体   繁体   English

来自Java应用程序和存储过程MSSQL的连接

[英]Connection from Java Application and Stored Procedure MSSQL

I have the stored procedure in SQL Sever and it has a few parameter. 我在SQL Sever中有存储过程,它具有一些参数。 I would like to give the value of parameter from the combo box (in java application). 我想从组合框中给出参数的值(在Java应用程序中)。 I've read this code (look at below) 我已经阅读了这段代码(请看下面)

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

But i didn't get the meaning. 但是我没有意思。 Is there any tutorial that give me some example just like in my case? 有没有像我一样的教程可以给我一些例子? Thanks for any reply 感谢您的回复

PreparedStatement pstmt = con.prepareStatement("{call dbo.uspGetEmployeeManagers(?)}");
  pstmt.setInt(1, 50);
  ResultSet rs = pstmt.executeQuery();

1) Line 1 creates a prepare statement object with your Stored Procedure. 1)第1行使用您的存储过程创建一个prepare语句对象。 The ? is the placeholder for the input parameter to the Stored Procs 2) Line 2 sets the input param to the stored proc 3) executeQuery executes the stored proc by providing the input and get the output as a resultset. 是存储过程输入参数的占位符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();

Above lines iterate over the result set and print each record 上面的行遍历结果集并打印每条记录

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM