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 }