Question

I am trying to get results from my stored procedure and I am getting ERROR: Incorrect syntax near '{'. . I saw many examples using the same thing for a sql string and I am not sure why I am getting an error..I have the same amount of parameters...

when I execute my stored procedure om my database like this

exec rptGetAssetbyLocation '2122' I get my 5 columns

how do I get results back using Callable Statement ? I am doing something wrong with a registerOutParameter ?

try
        {
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                                String url = "jdbc:sqlserver:...";
                                String username = "username";
                                String password = "password";

                    sb.append("<table id=\"examplee\" border=1 >" 
                    + "<tr><td>ID</td><td>TAG</td><td>NAME</td>"
                        + "<td>MODEL</td><td>SERIAL NUMBER</td></tr>");

            String query = "{exec rptGetAssetbyLocation(?,?,?,?,?)}";
      Connection conn = DriverManager.getConnection(url, username, password);
                    CallableStatement stmt = conn.prepareCall(query);
                    stmt.registerOutParameter(1, java.sql.Types.INTEGER);
                    stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
                    stmt.registerOutParameter(3, java.sql.Types.VARCHAR);
                    stmt.registerOutParameter(4, java.sql.Types.VARCHAR);
                    stmt.registerOutParameter(5, java.sql.Types.VARCHAR);


                                ResultSet rs = stmt.executeQuery();


            while (rs.next())
            {
                int id = rs.getInt("ID");
                String tag = rs.getString("tag");
                String name = rs.getString("name"); 
                String model = rs.getString("model"); 
                String serNum = rs.getString("serialNumber");

                sb.append("<tr><td>" + id + "</td>" 
                        + "<td>" + tag + "</td>" 
                        + "<td>" + name + "</td>" +
                        "<td>" + model + "</td>" +
                        "<td>" + serNum + "</td></tr>");
            }
            sb.append("</table>");
        }
        catch(Exception e)
        {
            sb.append("<h1>ERROR: " + e.getMessage() + "</h1>");
        }
        sb.append("</body></html>");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(sb);
    }
Was it helpful?

Solution

If all you do to run your Stored Procedure in SQL Server Management Studio (SSMS) is

exec rptGetAssetbyLocation '2122' 

then you probably don't need to mess with output parameters, you can just use the one input parameter to call your Stored Procedure and then retrieve the results from the ResultSet like this

String query = "exec rptGetAssetbyLocation ?";
Connection conn = DriverManager.getConnection(url, username, password);
CallableStatement stmt = conn.prepareCall(query);
stmt.setString(1, "2122");
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
    int id = rs.getInt("ID");
    String tag = rs.getString("tag");
    String name = rs.getString("name"); 
    String model = rs.getString("model"); 
    String serNum = rs.getString("serialNumber");
    // ...and do other useful stuff
}

OTHER TIPS

In addition to the answer by Gord Thompson, you could also use the JDBC call escape for stored procedures.

JDBC escapes are very specific escapes defined in the JDBC standard to bridge differences between SQL implementations. The JDBC escapes are translated to the database server specific syntax.

For calling stored procedures the escape is:

{call procname(arg1, ..,)}

or for a stored procedure with a return value:

{? = procname(arg1, ...)}

Where arg1 etc is either a literal value or a parameter placeholder. See the CallableStatement apidoc. When prepared or executed, the driver will translate this to the database syntax (ie EXEC procname arg1, ... in the case of SQL server).

The syntax you tried to use in your question is a mix between a JDBC escape (the {...}) and the SQL Server specific syntax of executing a stored procedure.

Also the result of your stored procedure is probably not an out parameter, but a ResultSet, but this depends on the stored procedure definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top