2011-12-10 73 views
1

這似乎是一個經常遇到的問題,但沒有具體的答案。我從來沒有遇到過這個問題。該代碼是在這裏:Java SQL異常在index :: 1處缺少IN或OUT參數

public static Reservation retrieveReservation()throws IOException, 
    SQLException{ 
    Reservation testRsv = new Reservation(); 
    try { 
     Connection con = null; 

     Class.forName("oracle.jdbc.driver.OracleDriver"); 
     con=DriverManager.getConnection(
     "jdbc:oracle:thin:@wewe", 
     "ewew", 
     "sdsds"); 
     con.setAutoCommit(false); 
     try { 
     PreparedStatement prepareStatement = con.prepareStatement("SELECT * 
     FROM SMD_RESERVATION_INSTANCES WHERE id = ?"); 
     ResultSet rset = prepareStatement.executeQuery(); 
     prepareStatement.setString(1, localIDTest); 
     prepareStatement.executeUpdate(); 
     prepareStatement.close(); 
     con.commit(); 
     if(rset.next()){ 
      retrievedID = rset.getString("ID"); 
      Blob blob = rset.getBlob("RESERVATIONINST"); 
      status = rset.getString("STATUS"); 
      long blobLength = blob.length(); 
      int pos = 1; // position is 1-based 
      int len = 10; 
      byte[] bytes = blob.getBytes(pos, len); 
      InputStream is = blob.getBinaryStream(); 
      ObjectInputStream ois = new ObjectInputStream(is); 
      testRsv = (Reservation)ois.readObject(); 
     } 

     // System.out.println("Map Size: " + retrievedmap.size()); 
     rset.close(); 
     con.close(); 
     }catch(IOException ioe){ 
     System.err.print(ioe); 
     } 
    }catch(ClassNotFoundException cnfe){ 
     System.err.print(cnfe); 
    } 
    return testRsv; 
} 

它產生如下:

java.sql.SQLException: Missing IN or OUT parameter at index:: 1 

任何想法,爲什麼? 我已經重新檢查變量作爲參數,它是ID並且它不爲空並且具有一個值。

回答

4

你必須改變語句的順序,

PreparedStatement prepareStatement = con.prepareStatement("SELECT * FROM 
      SMD_RESERVATION_INSTANCES WHERE id = ?"); 
    prepareStatement.setString(1, localIDTest); 
    ResultSet rset = prepareStatement.executeQuery(); 

刪除這些語句(而你正在閱讀不關閉連接)。

prepareStatement.executeUpdate(); 
prepareStatement.close(); 
con.commit(); 
+0

感謝那些沒有工作:) –

2

你需要創建一個參數的聲明,執行它,只有經過您所設置的參數:

 PreparedStatement prepareStatement = con.prepareStatement("SELECT * FROM 
     SMD_RESERVATION_INSTANCES WHERE id = ?"); 
     ResultSet rset = prepareStatement.executeQuery(); 
     prepareStatement.setString(1, localIDTest); 

這應該工作:

 PreparedStatement prepareStatement = con.prepareStatement("SELECT * FROM 
     SMD_RESERVATION_INSTANCES WHERE id = ?"); 
     prepareStatement.setString(1, localIDTest); 
     ResultSet rset = prepareStatement.executeQuery(); 
+0

謝謝:)感謝幫助 –

相關問題