suchen

Heim  >  Fragen und Antworten  >  Hauptteil

java - apache commons 的dbutils如何返回新插入的主键值?

我用的是,oracle数据库,主键是序列.
看到dbutils工具中的insert可以返回主键值,但是我试了下却报错了

public void add2() throws SQLException {
        QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
        String sql="INSERT INTO ACCOUNT VALUES(ACCOUNT_SEQ.NEXTVAL,?,?,SYSDATE)";

        Object o=qr.insert(sql,new ScalarHandler(),"小黑",52.65);
        System.out.println(o);
    }

报错信息如下

Exception in thread "main" java.sql.SQLException: Wrong number of parameters: expected 3, was given 2 Query: INSERT INTO ACCOUNT VALUES(ACCOUNT_SEQ.NEXTVAL,?,?,SYSDATE) Parameters: [小黑, 52.65]
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:392)
at org.apache.commons.dbutils.QueryRunner.insert(QueryRunner.java:610)
at org.apache.commons.dbutils.QueryRunner.insert(QueryRunner.java:534)
at top.getcode.dbutils.DBUtils.QueryRunnerCRUD.add2(QueryRunnerCRUD.java:91)

PHP中文网PHP中文网2813 Tage vor1005

Antworte allen(0)Ich werde antworten

Keine Antwort
  • StornierenAntwort