2013-07-10 63 views
0

你好,我想從我的數據庫調用存儲過程,但爲什麼這個錯誤彈出我無法弄清楚,和教程是不是真的有希望。存儲過程設置PARAMS

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) 

回答

1

有幾件事會在這裏:

  1. 不要換你的語句在大括號
  2. 您使用cstmtpreparedStatement交替。你只需要一個。
  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(); 
     } 
    } 

} 
+0

在任何人的情況下仍然得到這個問題的SQL Server 2008的 上我有一個更新語句同樣的問題。 切換到jtds驅動程序[jtds.sourceforge.net](從微軟)和代碼工作正常 – ChrisR