我正在尝试将屏幕上的订单项数据添加到derby db表中。订单项位于具有3个字段(列)的行中。
我尝试使用addBatch()
,但得到空错误。没有addBatch()
,我不会出错。因此,我改为使用executeUpdate()
方法。问题在于,现在我在数据库中得到了相同内容的重复条目(最多2页)。还将空的订单项行发布到数据库中。我尝试使用if语句,但是该方法只是忽略它们。这是代码。我究竟做错了什么?我在日常工作中使用该软件程序,因此确实需要解决该问题。请。
public void updatePROPOSAL() throws SQLException{
//Connection conn =null;
// conn.setAutoCommit(false);
// DecimalFormat dc = new DecimalFormat("#,###.00");
String addSQL2 = "INSERT INTO MATERIALTBL (MATLINEITEM,
MATLINEITEMCOST, "
+ "MATLINEITEMQTY, MATMOSS, MATCRM)"
+ "VALUES (?,?,?,?,?)";
PreparedStatement pst = null;
try {
conn = DriverManager.getConnection("jdbc:derby:proposaldb");
int tubes = jtxtfld_TUBES.getText().length();
System.out.println(tubes);
if(tubes != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_TUBES.getText());
pst.setString(2, jlbl_TUBES_COST.getText());
pst.setString(3, jtxtfld_TUBES_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int coup = jtxtfld_COUP.getText().length();
System.out.println(coup);
if (coup != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_COUP.getText());
pst.setString(2, jlbl_COUP_COST.getText());
pst.setString(3, jtxtfld_COUP_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int cmtrs = jtxtfld_CMTRS.getText().length();
System.out.println(cmtrs);
if(cmtrs != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_CMTRS.getText());
pst.setString(2, jlbl_CMTRS_COST.getText());
pst.setString(3, jtxtfld_CMTRS_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int spaceone = jtxtfld_spaceONE.getText().length();
System.out.println(spaceone);
if (spaceone != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_spaceONE.getText());
pst.setString(2, jlbl_spaceONE_COST.getText());
pst.setString(3, jtxtfld_spaceONE_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int hmtrs = jtxtfld_HMTRS.getText().length();
System.out.println(hmtrs);
if (hmtrs != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_HMTRS.getText());
pst.setString(2, jlbl_HMTRS_COST.getText());
pst.setString(3, jtxtfld_HMTRS_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int spacetwo = jtxtfld_spaceTWO.getText().length();
System.out.println(spacetwo);
if(spacetwo != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_spaceTWO.getText());
pst.setString(2, jlbl_spaceTWO_COST.getText());
pst.setString(3, jtxtfld_spaceTWO_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int gmtrs = jtxtfld_GMTRS.getText().length();
System.out.println(gmtrs);
if (gmtrs != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_GMTRS.getText());
pst.setString(2, jlbl_GMTRS_COST.getText());
pst.setString(3, jtxtfld_GMTRS_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int spacethree = jtxtfld_spaceTHREE.getText().length();
System.out.println(spacethree);
if (spacethree != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_spaceTHREE.getText());
pst.setString(2, jlbl_spaceTHREE_COST.getText());
pst.setString(3, jtxtfld_spaceTHREE_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int emtrs = jtxtfld_EMTRS.getText().length();
System.out.println(emtrs);
if (emtrs != 0) {
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_EMTRS.getText());
pst.setString(2, jtxtfld_EMTRS_COST.getText());
pst.setString(3, jtxtfld_EMTRS_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int spacefour = jtxtfld_spaceFOUR.getText().length();
System.out.println(spacefour);
if (spacefour != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_spaceFOUR.getText());
pst.setString(2, jtxtfld_spaceFOUR_COST.getText());
pst.setString(3, jtxtfld_spaceFOUR_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int count = jtxtfld_COUNT.getText().length();
System.out.println(count);
if (count != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_COUNT.getText());
pst.setString(2, jlbl_COUNT_COST.getText());
pst.setString(3, jtxtfld_COUNT_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int xmtrs = jtxtfld_XMTRS.getText().length();
System.out.println(xmtrs);
if (xmtrs != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_XMTRS.getText());
pst.setString(2, jlbl_XMTRS_COST.getText());
pst.setString(3, jtxtfld_XMTRS_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int rptrs = jtxtfld_RPTRS.getText().length();
System.out.println(rptrs);
if (rptrs != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_RPTRS.getText());
pst.setString(2, jlbl_RPTRS_COST.getText());
pst.setString(3, jtxtfld_RPTRS_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int rcvrs = jtxtfld_RCVRS.getText().length();
System.out.println(rcvrs);
if (rcvrs != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_RCVRS.getText());
pst.setString(2, jlbl_RCVRS_COST.getText());
pst.setString(3, jtxtfld_RCVRS_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
int coll = jtxtfld_COLL.getText().length();
System.out.println(coll);
if (coll != 0){
pst = conn.prepareStatement(addSQL2);
pst.setString(1, jtxtfld_COLL.getText());
pst.setString(2, jlbl_COLL_COST.getText());
pst.setString(3, jtxtfld_COLL_QTY.getText());
pst.setString(4, null);
pst.setString(5, newCRM2);
pst.executeUpdate(addSQL2);
}
//conn.commit();
}catch (SQLException e){}
}
我想使用addBatch()
方法,但是它在Netbeans 8.0.2中似乎不起作用。如果您能以任何方式指导我,我将不胜感激。
答案 0 :(得分:0)
Solved the autobatch() problem. Had two problems. The null exception was because the
database wasn't being connected. The second problem was that I had too many repeats
of the pst statement. The empty spaces was an issue but I used the length of each
first column --if the length was more than zero then the line was added to the
database.
private void UpdateRecordTablePROPOSALMAT() throws SQLException, ParseException{
Connection conn ;
try {
conn = DriverManager.getConnection("jdbc:derby:proposaldb");
newCRM = jtxtfldCRM.getText();
String addSQL2 = "INSERT INTO MATERIALTBL (MATLINEITEM, MATLINEITEMCOST, "
+ "MATLINEITEMQTY, MATMOSS, MATCRM)"
+ "VALUES (?,?,?,?,?)";
PreparedStatement psts = conn.prepareStatement(addSQL2);
int tubes = String.valueOf(AMT7A).length();
if(tubes != 0){
psts.setString(1, String.valueOf(AMT7A));//Couplings Name
psts.setString(2, String.format("%.2f", AMT8A));//Couplings Cost
psts.setString(3, String.format("%.2f",AMT9A));//Couplings QTY
psts.setString(4, null);
psts.setString(5, newCRM);
psts.addBatch();
}
int coup = String.valueOf(AMT10A).length();
if(coup != 0){
psts.setString(1, String.valueOf(AMT10A));//Couplings Name
psts.setString(2, String.format("%.2f",AMT11A));//Couplings Cost
psts.setString(3, String.format("%.2f",AMT12A));//Couplings QTY
psts.setString(4, null);
psts.setString(5, newCRM);
psts.addBatch();
}
int cmtrs = String.valueOf(AMT13A).length();
if(cmtrs != 0){
psts.setString(1, String.valueOf(AMT13A));//Cold Water Meters Name
psts.setString(2, String.format("%.2f",AMT14A));//Cold Water Meters Cost
psts.setString(3, String.format("%.2f",AMT15A));//Cold Water Meters QTY
psts.setString(4, null);
psts.setString(5, newCRM);
psts.addBatch();
}
[ continue with more addBatch line items ]
int[] affectedRecords = psts.executeBatch();
}finally {
PreparedStatement psts =null;
if(psts != null) { }
}
}