我只是想知道如何在 netbeans 上使用 jtable,因为我一直在苦苦挣扎,而且我什么也做不了。请帮我
Mukuma
问问题
1670 次
2 回答
1
以下将对您有所帮助,它是一个 Netbeans 项目。
http://download.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-SimpleTableDemoProject.zip
于 2011-09-22T08:56:56.590 回答
1
你可以在谷歌上搜索教程,尤其是在 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;
如果要在界面运行或使用按钮时立即加载,可以将方法放在构造函数中。
但是有关更多信息和其他任何信息,请随时回复,我会为您提供帮助。
你也可以使用这个家伙频道,因为他非常好,他解释了如何做到这一点。
希望这可以帮助你。
于 2016-04-17T16:31:26.653 回答