DB2 SQL错误:SQLCODE = -117,SQLSTATE = 42802,SQLERRMC = null,DRIVER = 3.68.61

时间:2017-06-21 15:46:31

标签: java sql db2 prepared-statement sql-insert

我在使用Java开发并将记录插入DB2数据库时遇到了这个问题。

sqlInsert = "INSERT into SI_Orders (SellersNo,OrderDate,ShipDatePlanned,"
            + "Warehouse,OrderType,ShipToName,ShipToAddress1,"
            + "ShipToCity,ShipToProvState,ShipToPostalZip,ShipToCountry" + ") "
            + "VALUES (?, ?, ?,'01','DO','bob','a','toronto','ON','h0h0h0','CA')";

    try {
        con = dc.getConnection();
        con.setAutoCommit(false);
        db2Stmt = con.prepareStatement(sqlInsert);

        for (Order order : orderWrapper.orders) {
            save1 = con.setSavepoint();
            db2Stmt.setString(1, order.getOrderId());
            db2Stmt.setTimestamp(2, shipStnDateFormat( order.getOrderDate() ) );            
            db2Stmt.setString(3, null); // PONo
             /*
             * db2Stmt.setTimestamp(4, shipStnDateFormat(order.getShipByDate()));
             */
            db2Stmt.executeUpdate();    
            //con.commit();
        } // end of enhanced for each block
    } // end of try block

    catch (SQLException e1) {
        e1.printStackTrace();
        if (con != null) {
            try {
                System.err.print("Transaction is being rolled back.");
                con.rollback();
            } 

            catch (SQLException se) {
                se.printStackTrace();
            } 
        } // end of if(con != null) block
    } // end of catch block
    finally {
        // Clean-up code
        con.setAutoCommit(true);

        if (db2Stmt != null) {
            db2Stmt.close();
        }

        if (con !=null){
            con.close();
        }
    } // end of finally block
} // end of insertDB method

private Timestamp shipStnDateFormat(String shipByDate) {
    SimpleDateFormat shipStnDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    SimpleDateFormat standardDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String parsedFinalFormattedDate = null;
    Date finalDate = null;

    if (shipByDate == null)
        return new java.sql.Timestamp(0); // return a dummy value

    else {
        try {
            Date parsedShipByDate = shipStnDateFormat.parse(shipByDate); //
            parsedFinalFormattedDate = standardDateFormat.format(parsedShipByDate); //
            finalDate = standardDateFormat.parse(parsedFinalFormattedDate); //
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return new java.sql.Timestamp(finalDate.getTime());
    } // end of else statement
} // end of shipStnDateFormat method`

我收到此错误... DB2 SQL错误:SQLCODE = -117,SQLSTATE = 42802,SQLERRMC = null,DRIVER = 3.6in8.61

显然,我希望在不抛出SQL异常的情况下插入所有三列。

此外,这是我用来创建表的DDL,它显示了每列的数据类型以及是否强制执行NOT NULL约束。

第一列“OrderId”是一个标识列,所以我没有为它发出一个insert参数。

CREATE TABLE SI_Orders (
OrderId INTEGER NOT NULL generated always as identity (start with 0, 
increment by 1, no cache),
SellersNo VARCHAR(20) NOT NULL,
OrderDate Timestamp(0) NOT NULL,
PONo VARCHAR(20),
ShipDatePlanned Timestamp(0) NOT NULL,
CONSTRAINT PK_SI_Orders PRIMARY KEY (OrderId)
);

2 个答案:

答案 0 :(得分:1)

您正在设置:

db2Stmt.setString(3, null); // PONo

当您的模型声明该列时:

ShipDatePlanned Timestamp(0) NOT NULL,

因此不允许将其设置为NULL,将参数3更新为非NULL值应解决此问题。

答案 1 :(得分:1)

要向查询添加列,您可以执行以下操作:

sqlInsert = "INSERT into SI_Orders (SellersNo,OrderDate,ShipDatePlanned,PONo,"
        + "Warehouse,OrderType,ShipToName,ShipToAddress1,"
        + "ShipToCity,ShipToProvState,ShipToPostalZip,ShipToCountry" + ") "
        + "VALUES (?, ?, ?,?,'01','DO','bob','a','toronto','ON','h0h0h0',?)";

所以在字符串中的随机位置添加一列,并添加一个?在VALUES中添加列。

指数?以1开头,仅匹配替换参数的列名称。

例如:在此示例中,ShipToCountry将为索引5