Java PreparedStatement RETURN_GENERATED_KEYS无效

时间:2012-06-19 17:02:41

标签: java sql jdbc insert prepared-statement

我正在尝试在执行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

提前致谢 道格

4 个答案:

答案 0 :(得分:3)

假设参数/参数是平衡的

(请确认查询本身执行,并且驱动程序支持RETURN_GENERATED_KEYS),

您是否可以尝试将RETURN_GENERATED_KEYS用作executeUpdate调用的参数的一部分?

pstmt = con.createStatement();
pstmt.executeUpdate(sql.toString(), Statement.RETURN_GENERATED_KEYS);

编辑: 只需阅读有关使用DB2的说明。根据{{​​3}}

_Restriction:对于JDBC和SQLJ版本3.57或更高版本的IBM数据服务器驱动程序,以下表单对于将行插入DB2®forz /OS®数据服务器上的视图无效。

各种Connection.prepareStatement(SQL语句,   Statement.RETURN_GENERATED_KEYS); _

答案 1 :(得分:1)

这种方式对我有用:

prepStmt = con.prepareStatement(sql, new String[]{"NameOfIDField"});

我曾经遇到过oracle db的问题,如果表有很多字段,那么这个问题就不起作用了。 但即使有60个领域,上述内容对我也有用。

答案 2 :(得分:1)

我的JT400.jar文件是旧版本。我从sourceforge下载了最新的jar文件,问题解决了。

答案 3 :(得分:0)

尝试使用Statement

Statement st    = null;
ResultSet rs    = null;
st = con.createStatement();

String query = " INSERT INTO refac_folios
([usuario], [estatus]) VALUES ('" + usuario + "'," + Vista_FoliosRefacciones.ESTATUS_CREADO )" ;

Integer afectadas = st .executeUpdate( query, Statement.RETURN_GENERATED_KEYS );

if (afectadas > 0){

     rs = st.getGeneratedKeys();

     if (rs .next()) {
          folio = rs.getInt(1);
     }
  rs.close();
}
st.close();