package jtableconnectiondb;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.JTable;
import java.awt.*;
import javax.swing.JScrollPane;
import java.sql.*;
import javax.swing.table.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class Frame1 extends JFrame {
JPanel contentPane;
String[] clos = {"job_id", "job_desc", "min_lvl", "max_lvl"};
String[][] data = new String[0][4];
DefaultTableModel dm=new DefaultTableModel(data,clos);
JTable jTable1 = new JTable(dm);
Vector vect;
JTableHeader jTableHeader1 = jTable1.getTableHeader();
JButton btn = new JButton();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jTable1.setBounds(new Rectangle(19, 11, 358, 168));
jTableHeader1.setBounds(new Rectangle(18, 12, 355, 20));
btn.setBounds(new Rectangle(307, 203, 66, 33));
btn.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
btn.setText("按钮");
contentPane.add(jTable1);
contentPane.add(btn);
contentPane.add(jTableHeader1);
}
public void btn_actionPerformed(ActionEvent e) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:MyODBC");
Statement st=conn.createStatement();
String sql="SELECT *FROM jobs";
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
vect.addElement(new Integer(rs.getInt("job_id")));
vect.addElement(rs.getString("job_desc"));
vect.addElement(new Integer(rs.getInt("min_lvl")));
vect.add(new Integer(rs.getInt("max_lvl")));
dm.addRow(vect);
vect.removeAllElements();
}
}
catch(Exception ee){
}
}
}