java - 有没有办法将多个表数据保存到mysql

标签 java swing jdbc

我有一个 Java 表格,其中包含多行,但是当我保存数据时,仅保存最后一行,其余行不会保存。

下面是我的代码:

try {
    int rows=jTable1.getRowCount();
    for (int row = 0; row < rows; row++) {
        String qtys = (String) jTable1.getValueAt(row, 0);
        String name = (String) jTable1.getValueAt(row, 1);
        String commo = (String) jTable1.getValueAt(row, 2);
        String wei = (String) jTable1.getValueAt(row, 3);

        String queryco = "Insert into invoice(qtys,name,commo,wei) " +
                         "values ('"+qtys +"','"+name+"','"+commo+"','"+wei+"')";

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

最佳答案

package com.sample;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;



public class JTableExample {

JFrame f; 
// Table 
JTable j; 


void process()  throws Exception
{ 
    // Frame initiallization 
    f = new JFrame(); 

    // Frame Title 
    f.setTitle("JTable Example"); 

    // Initializing the JTable 
    j = new JTable(); 
    j.setBounds(30, 40, 200, 300); 

    DefaultTableModel dtm = new DefaultTableModel(0, 0);

    // add header of the table
    String header[] = new String[] { "Prority", "Task Title", "Start",
    };

    // add header in table model     
    dtm.setColumnIdentifiers(header);

    for (int count = 1; count <= 1000; count++) {
        dtm.addRow(new Object[] { "data"+count, "data"+count, "data"+count});
    }

    j.setModel(dtm);


    // adding it to JScrollPane 
    JScrollPane sp = new JScrollPane(j); 
    f.add(sp); 
    // Frame Size 
    f.setSize(500, 200); 
    // Frame Visible = true 
    f.setVisible(true); 

    int rows=j.getRowCount();
    Class.forName("com.mysql.jdbc.Driver");

    Connection connection = 
    DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila",
    "root","root");

    String sqlQuery = "Insert into sakila.student(name,dep,rollnumber) 
    values(?,?,?)";

    PreparedStatement  stmt = connection.prepareStatement(sqlQuery);
    try {
        for(int row = 0; row<rows; row++)
        {   
            System.out.println("Count:"+row);
            stmt.clearParameters();
            String name= (String) j.getValueAt(row, 0);
            String rollNumber = (String) j.getValueAt(row, 1);
            String dep = (String) j.getValueAt(row, 2);

            stmt.setString(1, name);
            stmt.setString(2, dep);
            stmt.setString(3, rollNumber);
            stmt.executeUpdate();
        }

    } catch(Exception e) {
        System.out.println(e.getMessage());
    }
    connection.close();
} 

// Driver  method 
public static void main(String[] args)  throws Exception
{   
    JTableExample jt = new JTableExample(); 
    jt.process();
} }

我使用了这段代码,可以毫无问题地将 5000 行存储到数据库中。所有数据已成功存入数据库。

关于java - 有没有办法将多个表数据保存到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56072054/

相关文章:

按键时 Javafx imageView 旋转

java - 尝试将 BLOB 作为 InputStream 读取,但出现连接关闭错误。 Spring3 getJdbcTemplate() 方法

python - 通过python连接到Oracle数据库

java - Java I/O 流过滤器示例

java - Mulesoft 3.8.1中的这个选择异常策略有问题吗?

java - 第一个 JFrame 应该使用 'invokeLater' 创建还是可以直接从 main 创建?

java - Java 中的三态复选框

java swing更新问题

java - 如何在 SQL Server 中的 failoverPartner 服务器的 JDBC 连接 URL 中指定端口

java - 动态创建 FormLayout 行和列(在运行时)