【问题标题】:Java PreparedStatement RETURN_GENERATED_KEYS not workingJava PreparedStatement RETURN_GENERATED_KEYS 不起作用
【发布时间】:2012-06-19 17:02:41
【问题描述】:

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

提前致谢 道格

【问题讨论】:

  • 您能发布其余的相关代码吗?还请包括使用的数据库。
  • 我为您添加了更多代码。我在 websphere 中并连接到 iSeries 上的 IBM DB2 数据库。
  • 什么驱动?我已经很久没有使用过 DB2 ...
  • 我们使用的驱动是 DB2 UDB for iSeries (Toolbox)

标签: java sql jdbc insert prepared-statement


【解决方案1】:

假设参数/参数是平衡的

(请确认查询是本机执行的,并且驱动程序支持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 版本 3.57 或更高版本,以下形式对于在 DB2® for z/OS® 数据服务器上的视图中插入行无效。

Connection.prepareStatement(sql-statement, Statement.RETURN_GENERATED_KEYS);_

【讨论】:

  • 我已经使用你的例子进行了测试,我得到了同样的错误。
  • 我没有插入视图。这是一个物理文件。
  • 如果驱动程序支持 RETURN_GENERATED_KEYS(从您提到的备用执行路径中可以看出),那么逐步调试似乎是唯一可行的选择
【解决方案2】:

这样对我有用:

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

我曾经遇到过 oracle 数据库的问题,如果表有很多字段,这将无法正常工作。 但即使有 60 个字段,上述内容也适用于我。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      用语句试试

      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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 2021-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        相关资源
        最近更新 更多