我编写了代码,用于将值从jtable插入到derby数据库中。代码确实以有趣的方式插入值。因为我有7列代码插入一个单独的条目7次,第一行插入缺少一个值,第二个插入错过2个值,依此类推......这里是下面的代码。
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection con = DriverManager.getConnection("jdbc:derby:MTD","herbert","elsie1*#");
pst = con.prepareStatement("Insert into APP.ASSESSOR" +
"(EMPLOYEEID, NAME, SURNAME,ASSESSORNUMBER," +
"UNITSTANDARDNUMBER, DESCRIPTION, CREDITS, NQFLEVEL)"+
"values(?, ?, ?, ?, ?, ?, ?, ?)");
TableModel tm = jTable1.getModel();
for(int row = 0; row < tm.getRowCount(); row++){
for(int col = 0; col < tm.getColumnCount(); col++){
Object val = tm.getValueAt(row, col);
pst.setObject(col+1, val);
pst.addBatch();
}
}
pst.executeBatch();
JOptionPane.showMessageDialog(null, "Record successfully saved..!");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
我哪里错了。
答案 0 :(得分:0)
你应该将addBatch()
移到内部循环的一边。首先你应该将值设置为语句.ones你添加所有列调用addBatch()
for(int col = 0; col < tm.getColumnCount(); col++){
Object val = tm.getValueAt(row, col);
pst.setObject(col+1, val);
}
pst.addBatch();