Java中使用JTable 显示数据
时间:2016-10-14 16:51 来源:未知 作者:机器AI 点击:次
1.创建向量Vector保存数据库中数据; 2.创建向量保存每列标题; 3.创建表格数据模型DefaultTableModel; 4.创建表格对象JTable; 5.将JTable加入到课滚动的面板JScrollPane中; 6.将JScrollPane加入到JFrame中。 Admin.java: 程序代码 package cn.edu.ahau.mgc.swing; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class Admin extends JFrame{ Admin() { Vector row = null; DefaultTableModel dtm = new DefaultTableModel(); String[] tableTitles = {"学号", "姓名", "性别", "年龄" ,"专业"}; Vector tableTitle = new Vector(); for (int i = 0; i < tableTitles.length; i++) { tableTitle.add(tableTitles[i]); } ActionRow ar = new ActionRow(); ar.init(); row = ar.getRow(); dtm.setDataVector(row, tableTitle); JTable jt = new JTable(dtm); JScrollPane jsp = new JScrollPane(jt); this.setBounds(400, 300, 400, 300); this.getContentPane().add(jsp); this.setVisible(true); this.addWindowListener(new WindowMonitor()); } class WindowMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0); } } public static void main(String args[]) { new Admin(); } } ActionRow.java: 程序代码 package cn.edu.ahau.mgc.swing; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; public class ActionRow { private Vector row; public void init() { row = new Vector(); Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = ""; try { conn = DB.getConn(); sql = "select * from stu_stu"; stmt = DB.getStmt(conn); rs = DB.getRs(stmt, sql); while (rs.next()) { Vector col = new Vector(); col.add(rs.getInt(1)); col.add(rs.getString(2)); col.add(rs.getString(3)); col.add(rs.getInt(4)); col.add(rs.getString(5)); this.row.add(col); } } catch (SQLException e) { e.printStackTrace(); } finally { DB.close(rs); DB.close(stmt); DB.close(conn); } } public Vector getRow() { return row; } public void setRow(Vector row) { this.row = row; } } DB.java: 程序代码 package cn.edu.ahau.mgc.swing; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DB { public static Connection getConn() { Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:mgc", "system", "admin"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static Statement getStmt(Connection conn) { Statement stmt = null; try { stmt = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return stmt; } public static PreparedStatement getPstmt(Connection conn, String sql) { PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static ResultSet getRs(PreparedStatement pstmt) { ResultSet rs = null; try { rs = pstmt.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return rs; } public static ResultSet getRs(Statement stmt, String sql) { ResultSet rs = null; try { rs = stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void close(Connection conn) { try { if (conn != null) conn.close(); conn = null; } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement stmt) { try { if (stmt != null) stmt.close(); stmt = null; } catch (SQLException e) { e.printStackTrace(); } } public static void close(PreparedStatement pstmt) { try { if (pstmt != null) pstmt.close(); pstmt = null; } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet rs) { try { if (rs != null) rs.close(); rs = null; } catch (SQLException e) { e.printStackTrace(); } } (责任编辑:机器AI) |