'@PO'附近的语法不正确

时间:2018-08-15 12:06:04

标签: java sql-server netbeans-8

嘿,我正在使用Netbeans在sql中使用以下代码存储过程

 public void updateStock(String nameOrId, float stock) throws ClassNotFoundException, SQLException {
    ArrayList<Integer> i = new ArrayList<Integer>();
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    //Connection establishment
    con = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-CU5U75J\\SQLSERVER1;databaseName=GroceryWithJava", "Fateh", "Fateh");
    //Statement Object
    CallableStatement cst = null;
    Boolean name_ID = isNumeric(nameOrId);
    if (name_ID) {
        cst = con.prepareCall("EXEC IncStock(?,?)");
        cst.setInt(1, Integer.parseInt(nameOrId));
        cst.setFloat(2, stock);
        cst.execute();
    } else {
        cst = con.prepareCall("EXEC spGetProductID(?)");
        cst.setString(1, nameOrId);
        cst.execute();
        result = cst.getResultSet();
        int id = 1;
        while (result.next()) {
            i.add(result.getInt(1));
            if (i.size() <= 1) {
                id = i.get(1);
            }
        }
        cst = con.prepareCall("EXEC IncStock(?,?)");
        cst.setInt(1, id);
        cst.setFloat(2, stock);
    }
    cst.close();
}

我得到了以下结果

  

线程“ main”中的异常com.microsoft.sqlserver.jdbc.SQLServerException:'@ P0'附近的语法不正确。       在com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:259)       在com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1547)       在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:548)       在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement $ PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:479)       在com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7344)       在com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2713)       在com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:224)       在com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:204)       在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:463)       在杂货店.SqlLinking.updateStock(SqlLinking.java:204)       在杂货店.SqlLinking.main(SqlLinking.java:273)   C:\ Users \ Al Fateh \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml:53:返回的Java:1   失败(总时间:1秒)

我正在使用以下存储过程的Sql Server`

CREATE PROC IncStock( @id INT, @stock float)
AS
BEGIN
  UPDATE StockTB
      SET StockAvalaible=StockAvalaible+@stock WHERE ProductID=@id
END

第二个存储过程是:

--Stored Procedure to get Product ID From ProductTB
ALTER PROC spGetProductID(@Name varchar(MAX))
AS
BEGIN       
    SELECT ID FROM ProductTB WHERE Name=@Name       
END

如果我使用测试方法来测试这些程序在哪里工作或不起作用,它不会给出任何错误

ArrayList<Integer> i = new ArrayList<Integer>();
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        //Connection establishment
        con = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-CU5U75J\\SQLSERVER1;databaseName=GroceryWithJava", "Fateh", "Fateh");

        //Statement Object
        //CallableStatement cst = con.prepareCall("{call spGetProductID(?)}");
        CallableStatement cst = con.prepareCall("{call IncStock(?,?)}");
        cst.setInt(1, 14);
        cst.setFloat(2, (float) 2.3);
        cst.execute();
        /*result = cst.getResultSet();
        while (result.next()) {
            System.out.println(result.getInt(1));
            i.add(result.getInt(1));
        }*/
        cst.close();
    } catch (ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(null, ex.getCause(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}

在这里,我已经使用注释来测试这两个程序。

0 个答案:

没有答案