mysql vector查找,从mysql检索数据并将其放在JTables上

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值