我正在运行简单的java代码来从DB获取结果。但它总是让索引1超出范围。如何将变量设置为WHERE子句
public String getTransNumberFromDB(String cli) throws Exception {
System.out.println(clid);
String sql = "SELECT * FROM [MediaPrima_IVR].[dbo].[call_back] WHERE CLID= '?' and FLAG = '0'";
System.out.println(sql);
this.Con = DriverManager.getConnection(this.url, this.dbUName,
this.dbPswd);
PreparedStatement stmt = this.Con.prepareStatement(sql);
stmt.setString(1, clid);
ResultSet rs;
try {
rs = stmt.executeQuery();
while (rs.next()) {
getCLIFromResultSet(rs);
System.out.println("Getting Number from call back database...");
System.out.println("Number from call back database is:" + getNumber());
}
} catch (Exception e) {
System.out.println("Exception is : " + e);
throw e;
} finally {
stmt.close();
this.Con.close();
}
return getNumber();
}
private String getCLIFromResultSet(ResultSet rs) throws SQLException {
setNumber(rs.getString("CALL_ID"));
return Number;
}
但是我按如下方式返回了错误。
com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:698)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:707)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1015)
请帮助解决这个问题。
答案 0 :(得分:1)
?在sql中不应该在' ' (单引号)。它应该只是?。
String sql = "SELECT * FROM [MediaPrima_IVR].[dbo].[call_back] WHERE CLID= ? and FLAG = '0'";
答案 1 :(得分:-1)
String sql = "SELECT * FROM [MediaPrima_IVR].[dbo].[call_back] WHERE CLID= ? and FLAG = '0'";
System.out.println(sql);