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){
}
}
}
----------------解决方案--------------------------------------------------------
vect.addElement(new Integer(rs.getInt("max_lvl")));
这句话改正了!
----------------解决方案--------------------------------------------------------
你这程序有什么异常吗?
----------------解决方案--------------------------------------------------------
没有任何异常,就是点击按钮什么也不显示
----------------解决方案--------------------------------------------------------
当然显示不了,你那里是怎么写的,写成那样能显示吗
----------------解决方案--------------------------------------------------------
我读的是速成班,刚学一个月,虽然每天拼命努力但是感觉还是太多不懂,请版主大哥指点一下,哪里需要修改
----------------解决方案--------------------------------------------------------
public void btn_actionPerformed(ActionEvent e) {
这个方法需要修改
还有
我希望你能从基础好好学,不要去速成什么
要知道欲速则不达的道理
学程序速成是不可能的
----------------解决方案--------------------------------------------------------
改了一下午了还是不行,版主能明确指点一下吗?
----------------解决方案--------------------------------------------------------
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();//你这里加了又除掉所有的元素干什么?把这句去掉
}
jTable1.repaint();//再加这一句
}
catch(Exception ee){
}
}
----------------解决方案--------------------------------------------------------
还是不行啊!!还是不显示
----------------解决方案--------------------------------------------------------