我在使用Java進行開發並將記錄插入DB2數據庫時遇到此問題。DB2 SQL錯誤:SQLCODE = -117,SQLSTATE = 42802,SQLERRMC = null,DRIVER = 3.68.61
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」是標識列,所以我沒有爲此發出插入參數。
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)
);
你查找什麼 「SQLCODE = -117,SQLSTATE = 42802」 是什麼意思? – bradimus