In my java program i want to copy javatable data and copied it in to the mysql table.for that i select on开发者_开发百科e table and select the contents of it then copy.how to copy the result set in to mysql?
getRowCount()
method returns the number of rows.
getValueAt(row, col)
returns the data.
int rows = jTable1.getRowCount();
int columns = jTable1.getColumnCount();
for (int row = 0; row < rows; r++) {
try {
rs.moveToInsertRow();
rs.updateInt("id", Integer.parseInt(jTable1.getValueAt(row, 0));
rs.updateString("name", jTable1.getValueAt(row, 1));
rs.updateString("phone", jTable1.getValueAt(row, 2));
.....
rs.insertRow();
} catch (SQLException sqle) {
...
}
}
This is a default model sample. Adjust it to your needs:
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{"10", "11", "12"}
},
new String [] {
"Title 1", "Title 2", "Title 3"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
精彩评论