Domanda

Sto cercando di ottenere risultati dalla mia stored procedure e sto ottenendo ERROR: Incorrect syntax near '{'..Ho visto molti esempi usando la stessa cosa per un sql string e non sono sicuro del motivo per cui ricevo un errore ... ho la stessa quantità di parametri ...

Quando eseguo la mia stored procedure om mio database come questa

exec rptGetAssetbyLocation '2122 'I Ottenere le mie 5 colonne

Come eseguire i risultati indietro utilizzando la dichiarazione chiaramente?Sto facendo qualcosa di sbagliato con un 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);
    }
.

È stato utile?

Soluzione

Se tutto ciò che fai per eseguire la procedura memorizzata in SQL Server Management Studio (SSMS) è

exec rptGetAssetbyLocation '2122' 
.

Allora probabilmente non è necessario scherzare con i parametri di output, è possibile utilizzare il parametro di input one per chiamare la procedura memorizzata e quindi recuperare i risultati dal Diretto come questo

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
}
.

Altri suggerimenti

Oltre a La risposta di Gord Thompson , è possibile utilizzare anche la connessione JDBC per le procedure memorizzate. .

Le scale JDBC sono sfocette molto specifiche definite nello standard JDBC per colmare le differenze tra le implementazioni SQL. Le scale JDBC sono tradotte nella sintassi specifica del server di database.

Per chiamare le procedure memorizzate, la fuga è:

{call procname(arg1, ..,)}
.

o per una procedura memorizzata con un valore di ritorno:

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

Dove arg1 Etc è un valore letterale o un segnaposto parametro. Vedi il CallableStatement Apidoc. Se preparato o eseguito, il conducente si tradurrà nella sintassi del database (ovvero EXEC procname arg1, ... nel caso di SQL Server).

La sintassi che hai provato a utilizzare nella tua domanda è un mix tra una fuga JDBC ({...}) e la sintassi specifica di SQL Server per eseguire una stored procedure.

Anche il risultato della propria stored procedure non è probabilmente un parametro fuori, ma un ResultSet, ma questo dipende dalla definizione della procedura memorizzata.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top