package com.Swing; /** */ /** * @author Alex * @msg 数据库通过sql语句生成jtable */ import java.awt. * ; import java.sql. * ; import java.util. * ; import javax.swing. * ; import java.awt.event. * ; import javax.swing.table. * ; import com.Swing.FecthDate1; public class ListFrame extends JFrame ... { BorderLayout borderLayout1 = new BorderLayout(); DefaultTableModel dtm=new DefaultTableModel(); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); JScrollPane jScrollPane1 = new JScrollPane(); JTable jTable1 = new JTable(); JButton jButton1 = new JButton(); Vector v=new Vector(); Connection con; int columnCount; ResultSet rs; public ListFrame() ...{ try ...{ jbInit(); } catch (Exception exception) ...{ exception.printStackTrace(); } } private void jbInit() throws Exception ...{ this.setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().setLayout(borderLayout1); jPanel1.setLayout(null); jPanel2.setBounds(10,10,790,250); jPanel2.setLayout(null); jButton1.setBounds(new Rectangle(300, 270, 142, 24)); jScrollPane1.setBounds(new Rectangle(10, 10, 755, 240)); jButton1.setText("获取数据"); jButton1.addActionListener(new ListFrame_jButton1_actionAdapter(this)); this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); jTable1.setModel(dtm); jPanel1.add(jPanel2); jPanel1.add(jButton1); jPanel2.add(jScrollPane1); jScrollPane1.getViewport().add(jTable1); FecthDate1 fecth = new FecthDate1(); rs = fecth.Fecth("select * from tableui"); columnCount = rs.getMetaData().getColumnCount(); for (int i = 1; i <= columnCount; i++) ...{ dtm.addColumn(rs.getMetaData().getColumnName(i)); } rs.close(); this.setSize(800,350); this.setVisible(true); } public static void main(String[] args) ...{ ListFrame listframe = new ListFrame(); } public void jButton1_actionPerformed(ActionEvent actionEvent) ...{ try ...{ FecthDate1 fecth = new FecthDate1(); rs = fecth.Fecth("select * from tableui"); String[] s = new String[columnCount]; while (rs.next()) ...{ for (int i = 1; i < columnCount; i++) ...{ s[i - 1] = rs.getString(i); } dtm.addRow(s); } } catch (SQLException ex) ...{ System.out.println("1-->"+ex); ex.printStackTrace(); } }} class ListFrame_jButton1_actionAdapter implements ActionListener ... { private ListFrame adaptee; ListFrame_jButton1_actionAdapter(ListFrame adaptee) ...{ this.adaptee = adaptee; } public void actionPerformed(ActionEvent actionEvent) ...{ adaptee.jButton1_actionPerformed(actionEvent); } public static void main(String[] args)...{ ListFrame lf=new ListFrame(); //lf.setSize(300, 200); lf.setVisible(true); lf.pack(); }}