1、创建显示表格的窗体
package com.student.view; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; /* * 项目名称: * * 文件名称为:ShowStudent.java * 文件创建人:daxiang * * @author daxiang * @version * @time 2018年6月22日 上午8:03:24 * @copyright daxiang */ public class ShowStudent extends JFrame { private JPanel contentPane; private JTable table; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ShowTable frame = new ShowTable(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ShowStudent() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 682, 416); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); // 设置无布局 contentPane.setLayout(null); // 创建滚动面板 JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(27, 74, 603, 221); contentPane.add(scrollPane); // 创建表格 table = new JTable(); // 设置表格内容 table.setModel(new DefaultTableModel(new Object[][] { { null, null, null, null, null }, }, new String[] { "序号", "学号", "姓名", "性别", "班级" })); // 将表格加载到滚动面板 scrollPane.setViewportView(table); } }
这时已经将显示窗体创建成功,下面展示如何将数据库中的数据查询出来显示到表格中
2、在StudentDao增加查询方法
先从数据库中查询出所有的学生信息
public List<Student> getAll()throws SQLException{ DbUtil dbUtil = new DbUtil(); String sql="select * from tb_student"; ResultSet rs=dbUtil.executeQuery(sql); List<Student> list = new ArrayList<Student>(); while(rs.next()){ Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setSex(rs.getString("sex")); student.setSno(rs.getString("sno")); student.setClassName(rs.getString("classname")); list.add(student); } return list; }
3、在StudentSerivce中增加查询服务
在StudentDao提供了查询所有学生的操作,下面创建查询所有学生的服务
public List<Student> getStudent() throws SQLException { StudentDao studentDao = new StudentDao(); return studentDao.getAll(); }
4、将查询出来的数据关联到表格
package com.student.view; import java.awt.EventQueue; import java.sql.SQLException; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import com.student.model.Student; import com.student.service.StudentService; /* * 项目名称: * * 文件名称为:ShowStudent.java * 文件创建人:daxiang * * @author daxiang * @version * @time 2018年6月22日 上午8:03:24 * @copyright daxiang */ public class ShowStudent extends JFrame { private JPanel contentPane; private JTable table; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ShowTable frame = new ShowTable(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ShowStudent() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 682, 416); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); // 设置无布局 contentPane.setLayout(null); // 创建滚动面板 JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(27, 74, 603, 221); contentPane.add(scrollPane); // 创建表格 table = new JTable(); // 将默认的加载数据去掉,重新定义个表头 String[] head = new String[] { "序号", "学号", "姓名", "性别", "班级" }; // 将表格加载到滚动面板 scrollPane.setViewportView(table); try { // 创建服务 StudentService service = new StudentService(); // 查询出所有学生 List<Student> list = service.getStudent(); // 创建一个二维数组,5表示列数 Object[][] data = new Object[list.size()][5]; // 遍历 for (int i = 0; i < list.size(); i++) { data[i][0] = list.get(i).getId(); data[i][1] = list.get(i).getSno(); data[i][2] = list.get(i).getName(); data[i][3] = list.get(i).getSex(); data[i][4] = list.get(i).getClassName(); } // 将数据和表头封装 DefaultTableModel datamoModel = new DefaultTableModel(data, head); // 将封装好的数据加载 table.setModel(datamoModel); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }