实训内容
连接数据库MySQL和Java来公共实现一款可视化的学生管理系统。让该系统拥有登录界面,管理界面。
系统目录
需要创建的目录类
主体
大概代码展示
loginframe部分
代码展示
public class Main extends JFrame implements ActionListener{
public static void main(String[] args) throws IOException {
new Main();
final int WIDTH=800;
final int HEIGHT=500;
}
private JLabel queryLab = null;
private JTextField queryTxt = null;
private JButton queryBtn = null;
private JButton allBtn = null;
private JTable resultTb = null;
private JScrollPane jsp = null;
private JButton addBtn = null;
private JButton deleteBtn = null;
private JButton updateBtn = null;
private JPanel top = null;
private JPanel bottom = null;
private Model sm = null;
public Main() throws IOException {
super("学生管理系统");
queryLab = new JLabel("请输入姓名:");
queryTxt = new JTextField(10);
queryBtn = new JButton("查询");
allBtn = new JButton("全部");
queryBtn.addActionListener(this);
queryBtn.setActionCommand("query");
allBtn.addActionListener(this);
allBtn.setActionCommand("all");
addBtn = new JButton("添加");
deleteBtn = new JButton("删除");
updateBtn = new JButton("修改");
addBtn.addActionListener(this);
addBtn.setActionCommand("add");
deleteBtn.addActionListener(this);
deleteBtn.setActionCommand("delete");
updateBtn.addActionListener(this);
updateBtn.setActionCommand("update");
top = new JPanel();
top.add(queryLab);
top.add(queryTxt);
top.add(queryBtn);
top.add(allBtn);
bottom = new JPanel();
bottom.add(addBtn);
bottom.add(deleteBtn);
bottom.add(updateBtn);
sm = new Model();
String sql = "select * from stu";
sm.queryStu(sql, null);
resultTb = new JTable(sm);
jsp = new JScrollPane(resultTb);
this.add(top,BorderLayout.NORTH);
this.add(jsp,BorderLayout.CENTER);
this.add(bottom,BorderLayout.SOUTH);
this.setBounds(500,250,500,300);
// this.setIconImage(ImageIO.read(new File("D:\\yuandaima\\shishuo\\src\\studentManage\\img\\登录.png")));
this.setVisible(true);
this.setResizable(false);
}
//监听
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getActionCommand().equals("query")) {
String name = queryTxt.getText().trim();
if(name.length() != 0) {
String sql = "select * from stu where stuName=?";
String []paras = {name};
jtableUpdate(sql, paras);
} else {
JOptionPane.showMessageDialog(this, "姓名输入不能为空");
}
} else if(e.getActionCommand().equals("add")) {
new Add(this, "添加学生信息", true);
String sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("all")) {
String sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("delete")) {
int rowNum = this.resultTb.getSelectedRow();
if(rowNum == -1) {
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
String stuId = (String)sm.getValueAt(rowNum, 0);
String sql = "delete from stu where stuId=?";
String []paras = {stuId};
Model tmp = new Model();
tmp.cudStu(sql, paras);
sql = "select * from stu";
jtableUpdate(sql, null);
} else if(e.getActionCommand().equals("update")) {
int rowNum = this.resultTb.getSelectedRow();
if(rowNum == -1) {
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
new Set(this, "修改学生信息", true, sm, rowNum);
String sql = "select * from stu";
jtableUpdate(sql, null);
}
}
public void jtableUpdate(String sql, String[] paras)
{
sm = new Model();
sm.queryStu(sql, paras);
resultTb.setModel(sm);
}
}
运行结果
登录界面
实训总结
MySQL在连接Java时出现连接不上,代码敲出来,运行不出结果,代码老显示红色,并且很大内容需要依托老师的代码源。登录进学生管理系统后,有些程序无法运行,增加或者修改学生信息出错等,都是需要后面继续完善加强的。与Python的学生管理系统不同,Java和数据库的连接,使整个学生管理系统需要掌握的知识点增加,对代码运用熟练度要求都相对增加,在做学生管理系统时展现出来的困难都是需要在后面学习中解决的。