Java 将 Jtable 中的多行数据插入数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32755997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:01:06  来源:igfitidea点击:

Insert Mulitple Row Data from Jtable into database

javaswing

提问by francis romero

I am trying to save Multiple row data from JTable into Database, Here is my code for reference:

我正在尝试将 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());
}

The Problem is, it is only inserting one row data into database. Can someone please help me? :( thanks!

问题是,它只是将一行数据插入到数据库中。有人可以帮帮我吗?:( 谢谢!

采纳答案by Bhuwan Prasad Upadhyay

Remove following line codes from loop and place before loop

从循环中删除以下行代码并放置在循环之前

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);

Example:Replace your code by following code

示例:用以下代码替换您的代码

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 codesc = (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, codesc);
    pst.setString(4, coloc);
    pst.setString(5, coitemtagno);

    pst.addBatch();
}
pst.executeBatch();
conn.commit();
}
catch(Exception e){
    JOptionPane.showMessageDialog(this,e.getMessage());
}

Then run it think it work.

然后运行它认为它工作。

For Batch insert example is here https://my.vertica.com/docs/5.0/HTML/Master/14878.htm

批量插入示例在这里https://my.vertica.com/docs/5.0/HTML/Master/14878.htm

回答by kegs Production

the code above is not able to run in netbeans , However I made a version for netbeans.

上面的代码无法在 netbeans 中运行,但是我为 netbeans 制作了一个版本。

try{

  int rows=jTable1.getRowCount();

  for(int row = 0; row<rows; row++)
  {   
    Integer qty = (Integer)jTable1.getValueAt(row, 0);
    Double unitprice = (Double) jTable1.getValueAt(row, 1);
    String description = (String)jTable1.getValueAt(row, 2);
    Double total = (Double)jTable1.getValueAt(row, 3);
    String queryco = "Insert into invoice(qty,unitprice,description,total) values ('"+qty+"','"+unitprice+"','"+description+"','"+total+"')";

    pst = conn.prepareStatement(queryco);
    pst.execute();     
  }
  JOptionPane.showMessageDialog(null, "Successfully Save");
}
catch(Exception e){
  JOptionPane.showMessageDialog(this,e.getMessage());
}