中的多行数据插入数据库

中的多行数据插入数据库

本文介绍了将 Jtable 中的多行数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 JTable 中的多行数据保存到数据库中,这是我的代码供参考:

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

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!

推荐答案

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

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.

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

这篇关于将 Jtable 中的多行数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:53