蝴蝶不菲
调用任何一个setXxx()方法PreparedStatement,而sql查询字符串没有任何占位符。?为了这个。例如,这是不对:String sql = "INSERT INTO tablename (col1, col2, col3) VALUES (val1, val2, val3)";
// ...preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1, val1);
// Fail.preparedStatement.setString(2, val2);preparedStatement.setString(3, val3);您需要相应地修复SQL查询字符串以指定占位符。String sql = "INSERT INTO tablename (col1, col2, col3) VALUES (?, ?, ?)";
// ...preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1, val1);preparedStatement.setString(2, val2);
preparedStatement.setString(3, val3);注意,参数索引以1你知道不需要引用这样的占位符:String sql = "INSERT INTO tablename (col1, col2, col3) VALUES ('?', '?', '?')";否则,仍然会得到相同的异常,因为SQL解析器会将它们解释为实际的字符串值,因此无法找到占位符。另见:JDBC教程准备的语句