如何在netbeans上使用jtable

发布于 2024-07-24 15:44:56 字数 59 浏览 4 评论 0原文

我只是想知道如何在 netbeans 上使用 jtable,因为我正在苦苦挣扎,但我无能为力。 请帮我

I just wanna know how to use jtable on netbeans because I'm scruffing along and I could'nt do anything good. please, help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-07-31 15:44:56

您可以在 google 上搜索教程,尤其是在 YouTube 上。 这完全取决于您想对桌子做什么。

如果您想将 Jtable 连接到数据库(例如 MySQL),那么这里是编码。 我添加了注释,以便您可以轻松理解。

public final void loaddbtable() {
    DefaultTableModel model = (DefaultTableModel) t_View.getModel();
    //declared sql below used for database selection of specific information
    String sql = "Select * from tableName";
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        //connection for database
        Connection conn = (Connection)
                //root and username and password for access to the database
        DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseNameAsOnMySQL","root","password");
        //create the statement that will be used
        Statement stmt=conn.createStatement();
        //executes the statement
        ResultSet rs = stmt.executeQuery(sql); 
        //table to view data
        t_View.setModel(DbUtils.resultSetToTableModel(rs));    
    }
    catch (Exception e)
    {
        //exception handled for connection to database problem or data retrieval problem
        JOptionPane.showMessageDialog(null, "Error message if can't load", "Datatbase connection error", JOptionPane.ERROR_MESSAGE);
    }        
}

请记住,您将需要进行导入,例如

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

如果想在界面运行或使用按钮时立即加载,您可以将该方法放置在构造函数中。

但如需了解更多信息和其他任何信息,请随时回复,我会为您提供帮助。

你也可以使用这个家伙频道,因为他非常好,他准确地解释了如何做吧。

希望这可以帮助你。

You can rather search for tutorials on google, especially on YouTube. It all depends on what you would like to do with the table.

If you want to connect your Jtable to the database such as MySQL then here is the coding. I have added comments so that you can easily understand it.

public final void loaddbtable() {
    DefaultTableModel model = (DefaultTableModel) t_View.getModel();
    //declared sql below used for database selection of specific information
    String sql = "Select * from tableName";
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        //connection for database
        Connection conn = (Connection)
                //root and username and password for access to the database
        DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseNameAsOnMySQL","root","password");
        //create the statement that will be used
        Statement stmt=conn.createStatement();
        //executes the statement
        ResultSet rs = stmt.executeQuery(sql); 
        //table to view data
        t_View.setModel(DbUtils.resultSetToTableModel(rs));    
    }
    catch (Exception e)
    {
        //exception handled for connection to database problem or data retrieval problem
        JOptionPane.showMessageDialog(null, "Error message if can't load", "Datatbase connection error", JOptionPane.ERROR_MESSAGE);
    }        
}

Remember that you will need to have imports such as

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

You can place the method in your constructor if want to load immediately when the interface is run or using a button.

BUT for more information and anything else, feel free to reply and I will help you out.

you can also use this guys channel as he is very good and he explains exactly how to do it.

Hope this helps you out.

缱绻入梦 2024-07-31 15:44:56

以下将对您有所帮助,它是一个 Netbeans 项目。

http://download.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-SimpleTableDemoProject.zip

The following will help you, it is a Netbeans Project.

http://download.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-SimpleTableDemoProject.zip
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文