如何将 MySQL 表中的数据导入 Java JTable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7620492/
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
How to get data in MySQL table into Java JTable?
提问by SL_User
I'm working on Java project, and I need to load a particular set of data in to JTable
. Can someone explain to me how to do this? These are my fields in the "mrnform" table in database called "order_processing".
我正在处理 Java 项目,我需要将一组特定的数据加载到JTable
. 有人可以向我解释如何做到这一点吗?这些是我在名为“order_processing”的数据库中的“mrnform”表中的字段。
`Date` varchar(10) NOT NULL, `RegNo` int(11) NOT NULL, `Description` varchar(50) NOT NULL, `ItemNo` int(11) NOT NULL, `Unit` varchar(10) NOT NULL, `Quantity` int(11) NOT NULL, `Delivery_Date` varchar(10) NOT NULL, `Delivery_Address` varchar(10) NOT NULL, `Site_Name` varchar(30) NOT NULL,
回答by mKorbel
1) construct JDBC Connectionfor MySql, examples here
2) load data to the JTableby using TableModel, examples here
2)使用TableModel将数据加载到JTable 中,示例在这里
3) if you'll reall question, post this question here in ssccefrom
3)如果你雷尔的问题,在这里发布这个问题在SSCCE从
回答by adatapost
Pseudo code
伪代码
- Design the TableModel(or Vector)
- Establish the db connectionand retrieve result.
- Store database result into TableModel object.
- Construct the JTable(tableModel).
- 设计TableModel(或 Vector)
- 建立数据库连接并检索结果。
- 将数据库结果存储到 TableModel 对象中。
- 构造JTable(tableModel)。
回答by Headshota
Read the manual for the JTable:
阅读 JTable 的手册:
http://download.oracle.com/javase/tutorial/uiswing/components/table.html
http://download.oracle.com/javase/tutorial/uiswing/components/table.html
回答by V_Rajput
visit http://netshor.blog.com/2013/12/31/how-to-get-data-from-mysql-to-jtable/
'//initialize row of jTable int row=0; //start try-catch try{
//create connection with database //execute query //no start loop
while(rs.next()){jTable1.setValueAt(rs.getString(1), row, 0);
jTable1.setValueAt(rs.getString(2), row, 1);
jTable1.setValueAt(rs.getString(3), row, 2);
jTable1.setValueAt(rs.getString(4), row, 3);
jTable1.setValueAt(rs.getString(5), row, 4);
jTable1.setValueAt(rs.getString(6), row, 5);
jTable1.setValueAt(rs.getString(7), row, 6);
//increament in row of jtable. row++; } } catch(Exception e) {
}'
访问 http://netshor.blog.com/2013/12/31/how-to-get-data-from-mysql-to-jtable/
'//初始化jTable的行 int row=0; //开始try-catch try{
//创建与数据库的连接 //执行查询 //没有启动循环
while(rs.next()){jTable1.setValueAt(rs.getString(1), row, 0);
jTable1.setValueAt(rs.getString(2), row, 1);
jTable1.setValueAt(rs.getString(3), row, 2);
jTable1.setValueAt(rs.getString(4), row, 3);
jTable1.setValueAt(rs.getString(5), row, 4);
jTable1.setValueAt(rs.getString(6), row, 5);
jTable1.setValueAt(rs.getString(7), row, 6);
//在jtable的行中增加。行++;} } 捕获(异常 e){
}'