将多行行数据从Jtable插入数据库
我正在尝试将多行数据从JTable保存到数据库中,这是我的代码供参考:
try{ int rows=tblCO2.getRowCount(); for(int row = 0; row<rows; row++) { String coitemname = (String)tblCO2.getValueAt(row, 0); String cocateg = (String)tblCO2.getValueAt(row, 1); String codesc = (String)tblCO2.getValueAt(row, 2); String coloc = (String)tblCO2.getValueAt(row, 3); String coitemtagno = (String)tblCO2.getValueAt(row, 4); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/iotams",user,pass); conn.setAutoCommit(false); String queryco = "Insert into tblcheckout(CheckoutID,ItemTagNo,ItemName,Category,Description,Location) values (?,?,?,?,?)"; pst = conn.prepareStatement(queryco); pst.setString(1, coitemname); pst.setString(2, cocateg); pst.setString(3, codesc); pst.setString(4, coloc); pst.setString(5, coitemtagno); pst.addBatch(); } catch(Exception e) { } } pst.executeBatch(); conn.commit(); } catch(Exception e){ JOptionPane.showMessageDialog(this,e.getMessage()); }
问题是,它只是将一行数据插入数据库。 有人可以帮帮我吗? :( 谢谢!
从循环中删除以下行代码并在循环之前放置
Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/iotams",user,pass); conn.setAutoCommit(false); String queryco = "Insert into tblcheckout(CheckoutID,ItemTagNo,ItemName,Category,Description,Location) values (?,?,?,?,?)"; pst = conn.prepareStatement(queryco);
示例:通过以下代码替换代码
try{ int rows=tblCO2.getRowCount(); Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/iotams",user,pass); conn.setAutoCommit(false); String queryco = "Insert into tblcheckout(CheckoutID,ItemTagNo,ItemName,Category,Description,Location) values (?,?,?,?,?)"; pst = conn.prepareStatement(queryco); for(int row = 0; row
然后运行它认为它工作。
对于批量插入示例,请访问https://my.vertica.com/docs/5.0/HTML/Master/14878.htm
上面的代码无法在netbeans中运行,但是我为netbeans创建了一个版本。
try{ int rows=jTable1.getRowCount(); for(int row = 0; row
- org.apache.poi.POIXMLException:org.apache.poi.openxml4j.exceptions.InvalidFormatException:
- 警告:未知版本字符串。 将使用默认版本
- PHP,Java,Javascript之间的AES互操作性
- API SurveyQuestionImage.Data字段的正确格式是什么?
- 从多个线程写入文本文件?
- 如何在Hibernate中创建INSERT IGNORE查询?
- 当一些循环执行长任务时如何中断线程?
- Java绘画没有在Swing中绘制
- java.lang.ClassCastException:oracle.sql.CLOB无法强制转换为oracle.sql.CLOB