从mysql检索数据并将其放在JTables上 [英] retrieving data from mysql and putting it on JTables

查看:79
本文介绍了从mysql检索数据并将其放在JTables上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Java程序,该程序首先要求您输入用户名和密码,然后将其用于连接数据库.到目前为止,一切正常.现在,该程序就像一个客户/客户组织者.它具有客户加入的地址日期,电话号码等的名称和信息.然后单击客户,您可以在其上看到任何注释.但这不是现在的问题.

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.

我想做的是从我的表中获取信息,该表位于mysql数据库中,并使用JTable在Java中显示.然后我同时在JTable中编辑该信息,它还会更新数据库.任何有关如何执行此操作的建议将不胜感激.教程会更好.预先感谢..

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..

推荐答案

您可以使用通过操纵结果集创建的表模型来构建表.假设您正在使用JDBC,并且您已经了解如何使用结果集,则可能可以使用类似这样的内容::

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<String> columnNames = new Vector<String>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }

    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    while (res.next()) {
        Vector<Object> vector = new Vector<Object>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(res.getObject(columnIndex));
        }
        data.add(vector);
    }
    return new DefaultTableModel(data, columnNames);
}

如果这没有帮助,Stack上已经存在许多问题,您可以在其中引用...在提出问题之前,请尝试检查是否已回答您的问题!您可以检查以下内容:

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:

从MySQL DB检索数据并显示在JTable中

或者这个:

https://stackoverflow.com/search?q=fill+jtable+from+mysql

这篇关于从mysql检索数据并将其放在JTables上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆