JTable是一個表格組件,對JTable進行實例化時有多種方式,我個人比較喜歡用兩個鏈表集合進行傳參。
實例
沒連數據庫實例
1 package com.beekc.www; 2 import java.awt.*; 3 import java.util.*; 4 import javax.swing.*; 5 6 //繼承JFrame 7 public class Jtable extends JFrame { 8 9 //定義組件 10 JTable jTable = null; 11 JScrollPane jScrollPane = null; 12 13 //定義JTable的對象 14 Vector rowData,columnNames; 15 16 //定義一行數據的對象 17 Vector line1; 18 19 public static void main(String[] args) { 20 Jtable jTable = new Jtable(); 21 } 22 23 //構造函數 24 public Jtable() 25 { 26 //設置表格類目 27 columnNames = new Vector(); 28 columnNames.add("學號"); 29 columnNames.add("姓名"); 30 columnNames.add("籍貫"); 31 32 //設置表格數據 33 rowData = new Vector(); 34 line1 = new Vector(); 35 line1.add("001"); 36 line1.add("孫悟空"); 37 line1.add("花果山"); 38 rowData.add(line1); 39 40 line1 = new Vector(); 41 line1.add("002"); 42 line1.add("豬八戒"); 43 line1.add("天空"); 44 rowData.add(line1); 45 46 line1 = new Vector(); 47 line1.add("003"); 48 line1.add("沙悟凈"); 49 line1.add("大海"); 50 rowData.add(line1); 51 52 line1 = new Vector(); 53 line1.add("004"); 54 line1.add("唐三藏"); 55 line1.add("長安"); 56 rowData.add(line1); 57 58 //生成表格 59 jTable = new JTable(rowData,columnNames); 60 jScrollPane = new JScrollPane(jTable); 61 62 //添加組件 63 this.add(jScrollPane); 64 65 //窗體設置 66 this.setTitle("學生管理系統"); 67 this.setLocation(200,200); 68 this.setSize(280,200); 69 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 70 this.setVisible(true); 71 72 73 } 74 75 }
連接數據庫實例
1 package com.beekc.www; 2 import java.awt.*; 3 import java.sql.*; 4 import java.util.*; 5 import javax.swing.*; 6 7 //繼承JFrame 8 public class Student extends JFrame { 9 10 //定義組件 11 JTable jTable = null; 12 JScrollPane jScrollPane = null; 13 14 //定義JTable的對象 15 Vector rowData,columnNames; 16 17 //定義一行數據的對象 18 Vector line1; 19 20 //定義數據庫對象 21 Connection ct = null; 22 PreparedStatement ps = null; 23 ResultSet rs = null; 24 25 public static void main(String[] args) { 26 Student jTable = new Student(); 27 } 28 29 //構造函數 30 public Student() 31 { 32 //設置表格類目 33 columnNames = new Vector(); 34 columnNames.add("學號"); 35 columnNames.add("姓名"); 36 columnNames.add("籍貫"); 37 38 //設置表格數據 39 rowData = new Vector(); 40 41 try{ 42 //1.加載驅動 43 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 44 //2.得到連接 45 ct = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=beekc","sa","admin.."); 46 //3.創建介質方式 47 ps = ct.prepareStatement("select * from stu"); 48 49 //4.執行 50 rs = ps.executeQuery(); 51 52 while (rs.next()) 53 { 54 line1 = new Vector(); 55 line1.add(rs.getString(1)); 56 line1.add(rs.getString(2)); 57 line1.add(rs.getString(3)); 58 rowData.add(line1); 59 } 60 61 }catch (Exception e){ 62 e.printStackTrace(); 63 }finally { 64 65 } 66 67 //生成表格 68 jTable = new JTable(rowData,columnNames); 69 jScrollPane = new JScrollPane(jTable); 70 71 //添加組件 72 this.add(jScrollPane); 73 74 //窗體設置 75 this.setTitle("學生管理系統"); 76 this.setLocation(200,200); 77 this.setSize(280,200); 78 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 79 this.setVisible(true); 80 } 81 }

