如何在netbeans中使用jtable

时间:2009-06-15 10:58:48

标签: netbeans jtable

我只是想知道如何在netbeans上使用jtable,因为我正在努力,我不能做任何好事。拜托,帮帮我

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您可以在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;

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

但是有关更多信息和其他任何内容,请随时回复,我会帮助您。

你也可以使用this个人通道,因为他非常好,他解释了如何做到这一点。

希望这可以帮助你。