-1

jTable に入力された値を取得してデータベースに保存する方法を理解できません。誰でもコードを手伝ってもらえますか。

4

3 に答える 3

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+"')");

            }
于 2012-07-31T05:27:16.317 に答える
1

これを確認してください。その後、エラーが発生した場合は、このコミュニティが役立つように、コードとエラー行を配置します。これは、完全なコードを最初から要求する場所ではありません。

于 2012-07-31T04:57:46.847 に答える
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 + "'));
于 2012-07-31T05:18:31.390 に答える