Java 为SQL查询(DB2)使用PreparedStatement SQLCODE=-206 SQLSTATE=42703
您好,我在使用预处理语句进行SQL查询时遇到问题。我得到的错误如下: com.ibm.db2.jcc.am.ro:db2sql错误:SQLCODE=-206,SQLSTATE=42703,SQLERRMC=NAME,DRIVER=3.58.82 因此,显然使用我的变量Java 为SQL查询(DB2)使用PreparedStatement SQLCODE=-206 SQLSTATE=42703,java,sql,db2,prepared-statement,dao,Java,Sql,Db2,Prepared Statement,Dao,您好,我在使用预处理语句进行SQL查询时遇到问题。我得到的错误如下: com.ibm.db2.jcc.am.ro:db2sql错误:SQLCODE=-206,SQLSTATE=42703,SQLERRMC=NAME,DRIVER=3.58.82 因此,显然使用我的变量name存在问题,该变量包含用户输入的JTsextField的内容,或者该内容未保存在所述变量中。以下是我的SQL查询的重要类。我使用的是MVC模型和DAO模式 我的控制器类(仅显示重要部分): 我的BtrDao课程: public
name
存在问题,该变量包含用户输入的JTsextField的内容,或者该内容未保存在所述变量中。以下是我的SQL查询的重要类。我使用的是MVC模型和DAO模式
我的控制器类(仅显示重要部分):
我的BtrDao课程:
public interface BtrDao {
Collection<BTRBean> getBTR() throws SQLException, IOException;
}
public class AbstractDao {
public static Connection getConnection() throws SQLException {
return ConnectionManager.getInstance().getConnection();
}
public ResultSet getResultset(final String statement) throws SQLException {
final Statement stmnt = getConnection().createStatement();
return stmnt.executeQuery(statement);
}
public void closeConnection(final ResultSet resultset) throws SQLException {
if(resultset != null)
resultset.close();
getConnection().close();
}
}
我的ConnectionManager类:
public class ConnectionManager {
public Connection getConnection() throws SQLException {
try {
Properties props = new Properties();
FileInputStream in = new FileInputStream(".//required.\\dbprop.\\DBConn.properties");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if (drivers !=null)
System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
if (url !=null)
System.setProperty("jdbc.url", url);
String username = props.getProperty("jdbc.username");
if (username !=null)
System.setProperty("jdbc.username", username);
String password = props.getProperty("jdbc.password");
if (password !=null)
System.setProperty("jdbc.password", password);
return DriverManager.getConnection(url, username, password);
} catch (IOException e) {
throw new SQLException(e.getMessage(), e.getCause());
}
}
//Close connection
public void closeConnection(Connection con) throws SQLException {
if (con != null)
con.close();
}
// Making sure that there's only one instance of the class
private static ConnectionManager singleton_Instance;
// default constructor
private ConnectionManager() {
}
// returns the single instance of the class
public static ConnectionManager getInstance() {
if (singleton_Instance == null) {
singleton_Instance = new ConnectionManager();
}
return singleton_Instance;
}
}
占位符必须以
:
开头,因此您的查询必须如下所示:
String result = "SELECT BBSTBBNR, BBSTNABEG, BBSTPLZ FROM BP.TBBBST WHERE BBSTNABEG = :name AND BBSTPLZ = :plz";
String name = View.searchname.getText();
String plz = View.searchplz.getText();
// Use :<name> for parameters
String result = "SELECT BBSTBBNR, BBSTNABEG, BBSTPLZ FROM BP.TBBBST " +
" WHERE BBSTNABEG = :name AND BBSTPLZ = :plz";
DB2PreparedStatement preparedStatement = (DB2PreparedStatement) dbConnection.prepareStatement(result);
// Set values for parameters
preparedStatement.setJccStringAtName("name", name);
preparedStatement.setJccStringAtName("plz", plz);
您必须在准备语句后设置参数。您需要在查询中指出要使用哪些参数,并且需要显式设置值。JDBC API本身只支持位置参数,因此您需要将代码更改为:
String name = View.searchname.getText();
String plz = View.searchplz.getText();
// Use ? for parameters
String result = "SELECT BBSTBBNR, BBSTNABEG, BBSTPLZ FROM BP.TBBBST " +
" WHERE BBSTNABEG = ? AND BBSTPLZ = ?";
preparedStatement = dbConnection.prepareStatement(result);
// Set values for parameters
preparedStatement.setString(1, name);
preparedStatement.setString(2, plz);
如注释中mustaccio所示,IBMDB2JDBC驱动程序支持命名参数,请参阅。您的示例如下所示:
String result = "SELECT BBSTBBNR, BBSTNABEG, BBSTPLZ FROM BP.TBBBST WHERE BBSTNABEG = :name AND BBSTPLZ = :plz";
String name = View.searchname.getText();
String plz = View.searchplz.getText();
// Use :<name> for parameters
String result = "SELECT BBSTBBNR, BBSTNABEG, BBSTPLZ FROM BP.TBBBST " +
" WHERE BBSTNABEG = :name AND BBSTPLZ = :plz";
DB2PreparedStatement preparedStatement = (DB2PreparedStatement) dbConnection.prepareStatement(result);
// Set values for parameters
preparedStatement.setJccStringAtName("name", name);
preparedStatement.setJccStringAtName("plz", plz);
String name=View.searchname.getText();
字符串plz=View.searchplz.getText();
//用途:用于参数
String result=“从BP.TBBBST中选择BBSTBBNR、BBSTNABEG、BBSTPLZ”+
“其中BBSTNABEG=:name和BBSTPLZ=:plz”;
DB2PreparedStatement preparedStatement=(DB2PreparedStatement)dbConnection.preparedStatement(结果);
//设置参数的值
preparedStatement.setJccStringAtName(“name”,name);
preparedStatement.setJccStringAtName(“plz”,plz);
转换到
DB2PreparedStatement
可能并不总是可能的(例如在使用第三方连接池时)。您可能需要使用unwrap
(但这也不总是可能的)。如何设置参数?JDBC只支持位置占位符(?
)。@APL使用setString
函数:与非标准且不属于JDBC本身的NamedParameterStatement结合使用。现在它开始工作了。现在我只剩下一些改进,IBMJDBC驱动程序确实支持@jens提到的功能。