java 在 JTable 中简单显示数据库数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17913764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple show database data in JTable
提问by Sajad
I have a JTable
that have simple structure, I want to populate it with database data.
我有一个JTable
简单的结构,我想用数据库数据填充它。
I write this code, But now don't know to how to continue!
我写了这段代码,但现在不知道如何继续!
回答by camickr
Simple show database data in JTable
在 JTable 中简单显示数据库数据
There is no need to create a custom TableModel.
无需创建自定义 TableModel。
See the Table From Database Example
code in Table From Databasefor a simple way to load data into the DefaultTableModel and display the model in a JTable.
有关将数据加载到 DefaultTableModel 并在 JTable 中显示模型的简单方法,请参阅Table From Database 中的Table From Database Example
代码。
回答by Omid Faraji
this Exception is because of cols variable that didn't initialized anywhere.
这个异常是因为 cols 变量没有在任何地方初始化。
so you should initialize all of fields in TableModels constructor or in out of TableModel.
所以你应该初始化 TableModels 构造函数中或 TableModel 中的所有字段。
well , then you shouldn't using metaData or any kind of using database in TableModels main methods.
好吧,那么您不应该在 TableModels 的主要方法中使用 metaData 或任何类型的使用数据库。
just in constructor or out side of TableModel.
只是在构造函数中或在 TableModel 的外面。
like this. try:
像这样。尝试:
class d9tableModel extends AbstractTableModel {
Connection con;
Statement statement;
ResultSetMetaData metadata;
ResultSet resultSet;
String dbName = "mydb";
String bdUser = "root";
String dbPassword = "2323";
String dbUrl = "jdbc:mysql://localhost/mydb";
String query = "select * from mytable";
ArrayList<String> cols = new ArrayList<>();
ArrayList<ArrayList<String>> data = new ArrayList<>();
public d9tableModel() {
try {
con = DriverManager.getConnection(dbUrl, bdUser, dbPassword);
System.out.println("Connected to database successfully!");
statement = con.createStatement();
resultSet = statement.executeQuery(query);
int c = resultSet.getMetaData().getColumnCount();
for (int i = 0; i < c; i++) {
cols.add(resultSet.getMetaData().getColumnName(i));
}
while (resultSet.next()) {
ArrayList<String> row = new ArrayList<>();
for (int i = 0; i < c; i++) {
row.add(resultSet.getString(i));
}
data.add(row);
}
} catch (SQLException ex) {
System.out.println("Could not connect to database");
} finally {
try {
if (statement != null) {
statement.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(d9tableModel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return cols.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
ArrayList<String> row = data.get(rowIndex);
return row.get(columnIndex);
}
@Override
public String getColumnName(int column) {
return cols.get(column);
}}
this Table Model exactly maps your database table in your JTable.
这个表模型在你的 JTable 中准确地映射了你的数据库表。
sorry for my bad English every body.
对不起,我的英语不好,每个人。
good luck.
祝你好运。
回答by duffymo
Anyone who is so foolish as to include an empty catch block is sure to struggle:
任何愚蠢到包含一个空的 catch 块的人肯定会挣扎:
} catch (SQLException ex) {
}
This is a very bad idea. You should log or print the stack trace at minimum OR add a throws clause to the method signature and handle it elsewhere.
这是一个非常糟糕的主意。您应该至少记录或打印堆栈跟踪,或者在方法签名中添加一个 throws 子句并在其他地方处理它。
Your code style is poor, too. Readability matters. Develop a single coherent style and stick to it.
你的代码风格也很差。可读性很重要。形成一个统一的风格并坚持下去。