我试图在执行 SQL 插入时将标识列返回给我的 java 程序。运行代码时出现以下错误
Uncaught exception thrown in one of the service methods of the
servlet: Cocoon. Exception thrown : java.lang.AbstractMethodError: java/sql
/Connection.prepareStatement(Ljava/lang/String;I)Ljava/sql/PreparedStatement;
这是我正在运行的代码。
private void insertUserInputParameters(ReportData rptData){
UserInputParameters userParams = rptData.getUserInputData();
StringBuilder sql = new StringBuilder();
PreparedStatement pstmt = null;
ResultSet rs = null;
int userDataId = -1;
//Get a database connection.
sl = ServiceLocator.getInstance();
ds = sl.getDataSource("jdbc/collegeguide");
con = ds.getConnection();
con.setReadOnly(false);
sql.append("insert into cpgusrdtaf (statecd, addr1, addr2, city, state, ");
sql.append("zipcode, dependent, shdindic, marstatus, residency, prntatge, ");
sql.append("fincome, mincome, pincome, taxspaid, taxreturn, elig1040, ");
sql.append("gincome, pcash, inetwrth, bnetwrth, pbenefit, paddlinf, ");
sql.append("puntax, pdslcwrk, smstatus, sresidncy, studtr, stud1040, ");
sql.append("sadjinc, sincome, spincome, sdslcwrk, studtax, scash, ");
sql.append("sinvest, snetwrth, saddlinf, suntax, househld, nmbrsch, ");
sql.append("studact, studsat, schools, housing) ");
sql.append("values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ");
sql.append("?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
//This line of code is where I get the error**
pstmt = con.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
//If I remove the 'Statement.RETURN_GENERATED_KEYS' I do not get the error.**
pstmt = con.prepareStatement(sql.toString());
setStatementValues(pstmt, userParams);
pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
if(rs.next()){
userDataId = rs.getInt(1);
}
我不允许使用存储过程,所以我不能走那条路。任何帮助将不胜感激
我正在使用 java 1.5
提前致谢 道格
最佳答案
假设参数/参数是平衡的
(请确认查询在 native 执行,并且驱动程序支持 RETURN_GENERATED_KEYS),
您可以尝试使用 RETURN_GENERATED_KEYS 作为 executeUpdate 调用的参数的一部分吗?
pstmt = con.createStatement();
pstmt.executeUpdate(sql.toString(), Statement.RETURN_GENERATED_KEYS);
编辑: 只需阅读您关于使用 DB2 的说明。根据http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp?topic=%2Fcom.ibm.db2.luw.apdv.java.doc%2Fsrc%2Ftpc%2Fimjcc_t0057053.html
_Restriction:对于 IBM Data Server Driver for JDBC and SQLJ version 3.57 或更高版本,以下形式对于将行插入 DB2® for z/OS® 数据服务器上的 View 无效。
Connection.prepareStatement(sql语句, 声明.RETURN_GENERATED_KEYS);_
关于Java PreparedStatement RETURN_GENERATED_KEYS 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105815/