0

您好,我正在尝试从我的数据库中调用存储过程,但我无法弄清楚为什么会弹出此错误,并且教程并不是很有希望。

    public void setOrder(int quantity, String med, String section, String doc,
        String status) {

        CallableStatement cstmt = null;
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
                .newInstance();
    } catch (InstantiationException | IllegalAccessException
            | ClassNotFoundException e) {
        System.out.println("Failure intialization of the driver! ");
        e.printStackTrace();
    }
    String connectionUrl = "jdbc:sqlserver://localhost:1433;"
            + "databaseName=Pharmacy;user=sa;password=1234;integratedSecurity=true;";

    try {
        conn = DriverManager.getConnection(connectionUrl);
        cstmt = conn.prepareCall("{EXEC newOrder (?, ?, ?, ?)}");
    } catch (SQLException e) {
        System.out.println("Failure intialization of the connection! ");
        e.printStackTrace();
    }
    System.out.println("Connected... ");

    try {
        PreparedStatement preparedStatement = conn.prepareStatement("{EXEC newOrder (?, ?, ?, ?)}");

        preparedStatement.setInt(1, quantity);
        preparedStatement.setString(2, med);
        preparedStatement.setString(3, section);
        preparedStatement.setString(4, doc);

        ResultSet rs = null;

        rs = cstmt.executeQuery();


    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    finally{
        try {
            cstmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

错误:

com.microsoft.sqlserver.jdbc.SQLServerException: The value is not set for the parameter number 1.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildParamTypeDefinitions(SQLServerPreparedStatement.java:260)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildPreparedStrings(SQLServerPreparedStatement.java:219)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doPrepExec(SQLServerPreparedStatement.java:612)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:400)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:285)
    at repository.DataRepo.setOrder(DataRepo.java:106)
    at controller.DataCtrl.setOrder(DataCtrl.java:17)
    at view.DocUI$1.actionPerformed(DocUI.java:79)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

1 回答 1

1

这里发生了一些事情:

  1. 不要将你的语句用花括号括起来
  2. 您交替使用 acstmt和 a 。preparedStatement你只需要一个。
  3. 如果您的查询不返回结果集,请使用 execute 而不是 executeQuery
  4. 关闭连接,而不是语句。
  5. 您不能混合使用集成安全性和用户名/密码。使用其中一种

以下应该工作。但是,我现在没有办法测试它。

   public void setOrder(int quantity, String med, String section, String doc,
        String status) {

    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
                .newInstance();
    } catch (InstantiationException | IllegalAccessException
            | ClassNotFoundException e) {
        System.out.println("Failure intialization of the driver! ");
        e.printStackTrace();
    }
    String connectionUrl = "jdbc:sqlserver://localhost:1433;"
            + "databaseName=Pharmacy;user=sa;password=1234;";

    try {
        conn = DriverManager.getConnection(connectionUrl);
    } catch (SQLException e) {
        System.out.println("Failure intialization of the connection! ");
        e.printStackTrace();
    }
    System.out.println("Connected... ");

    try {
        PreparedStatement preparedStatement = conn.prepareStatement("EXEC newOrder (?, ?, ?, ?)");

        preparedStatement.setInt(1, quantity);
        preparedStatement.setString(2, med);
        preparedStatement.setString(3, section);
        preparedStatement.setString(4, doc);


        preparedStatement.execute();


    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    finally{
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
于 2013-07-11T01:15:19.943 回答