|
|
|
Java GUI อยากได้วิธีอ่านข้อมูลจาก Database มาแสดงที่ jTable ครับ |
|
|
|
|
|
|
|
Code (Java)
String sql = "select * from your_table";
try{
pst = conn.prepareStatement(sql);
rs = pst.executQuery();
jTable.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e{
JOptionPane.showMassegeDialog(null, e);
}
ต้องใช้ Libarly ด้วยนะครับ...
http://www.mediafire.com/download/708az84mw3424ba/tut+1-9.rar
มีอีกวิธียุ่งยากกว่านี้ครับ ลองวิธีนี้ดูก่อนนะครับ
|
|
|
|
|
Date :
2013-08-30 13:15:06 |
By :
ideasza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
|
|
|
|
|
Date :
2013-08-30 14:29:21 |
By :
ดล |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แล้วครับ แต่อยากให้มี Checkbok ใน JTable ด้วยครับ
|
|
|
|
|
Date :
2013-08-30 18:05:10 |
By :
ดล |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
กรณีต้องการเขียนเองครับ
Code (Java)
package com.java.myapp;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
public class MyForm extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
MyForm frame = new MyForm();
frame.setVisible(true);
}
});
}
/**
* Create the frame.
*/
public MyForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 242);
setTitle("ThaiCreate.Com Java GUI Tutorial");
getContentPane().setLayout(null);
// ScrollPane
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(64, 44, 440, 89);
getContentPane().add(scrollPane);
// Table
JTable table = new JTable();
scrollPane.setViewportView(table);
// Model for Table
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addColumn("CustomerID");
model.addColumn("Name");
model.addColumn("Email");
model.addColumn("CountryCode");
model.addColumn("Budget");
model.addColumn("Used");
Connection connect = null;
Statement s = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
"?user=root&password=root");
s = connect.createStatement();
String sql = "SELECT * FROM customer ORDER BY CustomerID ASC";
ResultSet rec = s.executeQuery(sql);
int row = 0;
while((rec!=null) && (rec.next()))
{
model.addRow(new Object[0]);
model.setValueAt(rec.getString("CustomerID"), row, 0);
model.setValueAt(rec.getString("Name"), row, 1);
model.setValueAt(rec.getString("Email"), row, 2);
model.setValueAt(rec.getString("CountryCode"), row, 3);
model.setValueAt(rec.getFloat("Budget"), row, 4);
model.setValueAt(rec.getFloat("Used"), row, 5);
row++;
}
rec.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(s != null) {
s.close();
connect.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
|
|
|
|
Date :
2013-09-01 10:53:50 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Java GUI Retrieve List Data from Database
|
|
|
|
|
Date :
2013-09-07 11:21:10 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|