从循环中删除以下行代码并将其放置在循环之前
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<rows; row++){ String coitemname = (String)tblCO2.getValueAt(row, 0); String cocateg = (String)tblCO2.getValueAt(row, 1); String presc = (String)tblCO2.getValueAt(row, 2); String coloc = (String)tblCO2.getValueAt(row, 3); String coitemtagno = (String)tblCO2.getValueAt(row, 4); pst.setString(1, coitemname); pst.setString(2, cocateg); pst.setString(3, presc); pst.setString(4, coloc); pst.setString(5, coitemtagno); pst.addBatch();}pst.executeBatch();conn.commit();}catch(Exception e){ JOptionPane.showMessageDialog(this,e.getMessage());}
然后运行它认为它起作用。
对于批量插入示例,请参见https://my.vertica.com/docs/5.0/HTML/Master/14878.htm
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)