如何从jTable获取值并存储在数据库中?

时间:2012-07-31 04:53:10

标签: java swing jdbc jtable

我无法理解如何获取在jTable中输入的值并将它们存储在数据库中。任何人都可以帮我解决这个问题。

3 个答案:

答案 0 :(得分:3)

这可用于从jTable获取值并插入数据库,然后再与数据库建立正确的连接。

dbStatement=con.createStatement();
for(int i=0;i<=jTable1.getRowCount;i++){

                String item=jTable1.getValueAt(i, 1).toString();
                String quant=jTable1.getValueAt(i,2 ).toString();
                String unit=jTable1.getValueAt(i, 3).toString();
                String tot=jTable1.getValueAt(i, 4).toString();

                dbStatement.executeUpdate("INSERT INTO tableName VALUES('"+item+"','"+quant+"','"+unit+"','"+tot+"')");

            }

答案 1 :(得分:1)

Check This 。之后任何错误然后将您的代码和错误行放在一起,以便此社区可以帮助您。这不是您从头开始要求完整代码的地方。

答案 2 :(得分:1)

为了从Jtable中检索数据,您可以使用最具体的方法:

Object value = jtable.getValueAt(rowIndex, columnIndex);

在代码流之后,您应该使用mysql打开数据库连接,例如:

// driver registration
Class.forName("com.mysql.jdbc.Driver");

// create a connection
Connection connection = DriverManager.getConnection("jdbc:mysql://server_name/database_name","user", "password");

// create a statement
Statement s = connection.createStatement();

// execute a query, this method will return the number of affected rows
int count = s.executeUpdate ("insert into some_table(value) values('" + value + "'));