MSSQL SELECT WHERE 子句中的语句错误
MSSQL SELECT Statement Error in WHERE clause
我是 运行 简单的 java 从数据库获取结果的代码。但它总是给出索引 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)
请帮忙解决这个问题。
? in sql 不应该在 ' '(单引号)中。应该只是?.
String sql = "SELECT * FROM [MediaPrima_IVR].[dbo].[call_back] WHERE CLID= ? and FLAG = '0'";
String sql = "SELECT * FROM [MediaPrima_IVR].[dbo].[call_back] WHERE CLID= ? and FLAG = '0'";
System.out.println(sql);
我是 运行 简单的 java 从数据库获取结果的代码。但它总是给出索引 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)
请帮忙解决这个问题。
? in sql 不应该在 ' '(单引号)中。应该只是?.
String sql = "SELECT * FROM [MediaPrima_IVR].[dbo].[call_back] WHERE CLID= ? and FLAG = '0'";
String sql = "SELECT * FROM [MediaPrima_IVR].[dbo].[call_back] WHERE CLID= ? and FLAG = '0'";
System.out.println(sql);