/**************
從emp表查出姓名和工作,放入jtable,我也新學的,例子簡陋,而且可能有些寫法還不規範
******************/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class RsToJtable {
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
JFrame f = new JFrame("Sql結果放入JTable");
JTable table = new JTable(0, 2);
DefaultTableModel dataModel = (DefaultTableModel) table.getModel();
int count = dataModel.getRowCount();
while (count > 0) {
dataModel.removeRow(0);
}
Connection conn = getConn();
PreparedStatement ps = conn
.prepareStatement("SELECT ename,job from emp");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String ename = rs.getString(1);
String job = rs.getString(2);
Vector<String> v = new Vector<String>();
v.add(ename);
v.add(job);
dataModel.addRow(v);
table.setModel(dataModel);
f.add(table);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
public static Connection getConn() throws ClassNotFoundException,
String url = "jdbc:oracle:thin:@127.0.0.1:1521/orcl";//連線
String username = "scott";//使用者名稱
String password = "scott";//密碼
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
return (conn != null) ? conn : null;
/**************
從emp表查出姓名和工作,放入jtable,我也新學的,例子簡陋,而且可能有些寫法還不規範
******************/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class RsToJtable {
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
JFrame f = new JFrame("Sql結果放入JTable");
JTable table = new JTable(0, 2);
DefaultTableModel dataModel = (DefaultTableModel) table.getModel();
int count = dataModel.getRowCount();
while (count > 0) {
dataModel.removeRow(0);
}
Connection conn = getConn();
PreparedStatement ps = conn
.prepareStatement("SELECT ename,job from emp");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String ename = rs.getString(1);
String job = rs.getString(2);
Vector<String> v = new Vector<String>();
v.add(ename);
v.add(job);
dataModel.addRow(v);
}
table.setModel(dataModel);
f.add(table);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static Connection getConn() throws ClassNotFoundException,
SQLException {
String url = "jdbc:oracle:thin:@127.0.0.1:1521/orcl";//連線
String username = "scott";//使用者名稱
String password = "scott";//密碼
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
return (conn != null) ? conn : null;
}
}