【发布时间】:2015-07-15 05:50:44
【问题描述】:
我编写了将值从 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);
}
我哪里错了。
【问题讨论】: