0

I have stored procedure that searches persons on basis of any given inputs (any bio data e.g blood group, name, identity no. etc. taking almost 26 inputs).

I am executing a stored procedure (sql server) from java using Callable Statement preparecall method. Previously I had a problem that my stored procedure was not giving any results (0 row result) whenever the string input was passed in one of its many input parameters. On other hand gave correct results when any integer type parameter value was passed but when I changed jdbc driver from jdbc:odbc driver to sqlserver driver (driver name=com.microsoft.sqlserver.jdbc.SQLServerDrive), the problem was solved.

Now my new problem is that when my procedure procSearch2, that is taking 16 inputs, is running it gives results perfectly fine with the same Connection variable of jdbc driver (changed one), but the other procedure, which have 27 inputs (procSearch1) is not giving any results even with int or string input values passed.

It does not give any error it just returns me 0 row result where it should return 3 rows result.

here is the code

 CallableStatement stmt=connect.prepareCall("{CALL  procSearch1(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
 stmt.setInt(1,0);
 stmt.setString(2,"");
 stmt.setString(3,"");
 .
 .
 .
stmt.setInt(27,0);
 ResultSet result=stmt.executeQuery();

 if(result!=null)
{
   System.out.println(result.getRow()); 
  while(result.next)
  {
    System.out.println(result.getString("Name"));

   }

 }
4

1 回答 1

0

您需要为 CallableStatement 设置一个输出参数来读取 sp 的返回值。像这样的东西(为简洁起见省略了异常处理):

CallableStatement  stmt = connection.prepareCall("{? = call procSearch1(?,?,?) }");
stmt.registerOutParameter(1, java.sql.Types.INTEGER);
stmt.setString(2, "abc");
stmt.setString(3, "xyz");
stmt.execute();
Integer sp_return_value = stmt.getInt(1);

编辑:根据您的评论,如果您的 SP 返回 Select 的结果,即 resultSet,我认为您需要使用 statement.execute() 而不是 executeQuery()。

请参阅http://msdn.microsoft.com/en-us/library/ms378630.aspxhttp://msdn.microsoft.com/en-us/library/ms378487.aspx

所以,你的代码应该是这样的:

CallableStatement stmt=connect.prepareCall("{CALL  procSearch1(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
stmt.setInt(1,0);
stmt.setString(2,"");
stmt.setString(3,"");
.
.
.
stmt.setInt(27,0);
ResultSet result = stmt.execute();

if(result!=null) {
    System.out.println(result.getRow()); 
    while(result.next) {
        System.out.println(result.getString("Name"));
    }
}
于 2012-09-02T16:03:21.407 回答