I'm trying to write a java program that first asks you for username and password, which are then used to connect to the database. That works fine so far. Now the program is like a customer/client organizer. it has the names and information of like address date that the customer joined, phone number etc etc. Then you click a customer and you can see any notes on them. But thats not the problem now.
what im trying to do is getting the information from my table which is in mysql database and display it in java with JTable. then at the same time i edit that information in the JTable, it also updates the database. any advice on how to do this would be thanked. Tutorials would be even better. thanks in advance..
解决方案
You can build the table using a Table Model created by manipulating a Result Set. You can probably use something like this, assuming that you're using the JDBC and you already understand how to use result sets... :
JTable table = new JTable(writeResult(res));
public static DefaultTableModel writeResult (ResultSet res) throws SQLException {
ResultSetMetaData metaData = res.getMetaData();
Vector columnNames = new Vector();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
Vector> data = new Vector>();
while (res.next()) {
Vector vector = new Vector();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(res.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
Edit: If this doesn't help, there are many questions that already exist on Stack in which you can reference... Please try and check to see if your question has already been answered before asking it! You can check this:
Or this: