这似乎是一个常见的问题,但没有具体的答案。我以前从未遇到过这个问题。代码在这里:
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的参数,它不是null并且有一个值。
答案 0 :(得分: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();
答案 1 :(得分: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();