实验目标
掌握Java窗体开发技术,了解信息管理系统开发过程,熟练运用JDBC驱动连接MySQL数据库,以及数据库表的增删改查操作。
系统需求
开发一个学生管理系统,能够实现简单的学生信息管理,学生信息存储到MySQL数据库,窗体程序为客户端,客户端能够连接数据库服务器,实现学生信息的增删改查操作。
本系统为简单的C/S结构,C端为Java窗体程序,S端为MySQL数据库服务器。
C端Java窗体程序提供主程序窗口、添加、更新、删除和查询学生信息窗口。主程序窗口提供系统管理和学生管理两个菜单,系统管理菜单用于退出程序,学生管理菜单用于学生信息的增加、修改、删除和查询。主程序窗口如下图所示。
老师选择学生管理菜单下的增加命令,系统需要打开添加学生信息的Java窗体,窗体如下图所示。
老师在增加学生信息窗体中录入学生的学号、姓名、年龄和年级信息,单击【增加学生信息】按钮可添加学生信息到数据库。
修改命令用于修改学生信息,老师输入学生学号,系统返回该学生的信息,并显示在Java窗体中,老师修改学生的信息,并提交新修改的学生信息到数据库。窗体界面参照添加学生信息窗体请自行设计。
删除命令用于删除学生信息,老师需要先查询学生的信息,学生的信息显示在窗体后,再单击【删除】按钮删除学生信息。
开发本系统需要在客户端或服务器端安装MySQL数据库,并创建数据库student,在student数据库创建stinfo表,表结构如下:
表格1 学生信息表
系统设计
创建一个主窗体类JMainClient,该类继承Jframe类,在窗体内部创建工具条和菜单。创建四个子窗体类JaddStudent、JUpdateStudent、JDelStudent、JQuery,分别用于添加学习信息、更新学生信息,删除学习信息和查询学生信息,在上述窗体内部分别创建四个编辑控件和一个按钮控件,编辑控件用户输入或显示学生的信息,按钮控件用于老师进行添加、更新、删除、查询的动作相应。
另外,系统还需要配置MySQL数据库JDBC驱动程序,本课程采用的是MySQL8.0数据库,JDBC驱动程序文件是mysql-connector-java-8.0.18.jar,JDBC驱动程序配置方法参见《使用JDBC连接MySQL数据库》一课。
系统功能构成见下图。
系统主要功能由添加学生信息、修改学生信息、删除学生信息、查询学生信息、数据库存取模块构成。
添加学生信息模块由JaddStudent类实现,修改学生信息模块由JUpdateStudent类实现,删除学生信息模块由JdelStudent类实现,查询学生信息模块由Jquery类实现,数据库存取模块由DataMamager类实现。
系统编程
DataMamager类用于数据库操作,主要功能是配置MySQL数据库连接参数,连接MySQL数据库,执行SQL查询,关闭数据库。
public class DataMamager {
static Connection conn = null;
static java.sql.Statement stmt;
static ResultSet rs;
static String sql;
//数据库连接操作方法
public static void OpenConn() throws Exception{
try {
Class.forName(“com.mysql.cj.jdbc.Driver”);
String url = “jdbc:mysql://localhost:3306/student?useSSL=false”;
String username = “root”; String password = “123456”;//密码是你安装mysql时的密码
conn = DriverManager.getConnection(url,username,password);
if(conn != null) System.out.println(“数据库连接成功”);
}catch(Exception e) {
System.err.println(“数据库连接:”+e.getMessage()+”\n”);
}
}
//执行数据查询的方法
public ResultSet executeQuery(String sql) {
stmt = null; rs = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}catch(SQLException e) {
System.err.println(“查询数据:”+e.getMessage());
}
return rs;
}
//执行创建数据表、插入数据等操作
public void execute(String sql) {
stmt = null; rs = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.execute(sql);
}catch(SQLException e) {
System.err.println(e.getMessage());
}
}
//更新数据库操作方法
public void executeUpdate(String sql) {
stmt = null; rs = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.execute(sql);
conn.setAutoCommit(false);
conn.commit();//提交
}catch(SQLException e) {
System.err.println(“更新数据”+e.getMessage());
}
}
//关闭statement对象的方法
public void closeStmt() {
try {
stmt.close();
}catch(SQLException e){
System.err.println(“释放对象”+e.getMessage());
}
}
//关闭数据库的方法
public void closeConn() {
try {
conn.close();
}catch(SQLException e) {
System.err.println(“释放对象”+e.getMessage());
}
}
}
JmainClient类是系统的主窗体类,它继承Jframe类并实现ActionListener接口,应用JmenuBar创建菜单栏,应用Jmenu创建菜单,使用JmenuItem创建菜单项。
public class JMainClient extends JFrame implements ActionListener {
JMenuBar mainMenu = new JMenuBar(); //创建菜单栏
JMenu menuSystem = new JMenu(“系统管理”);
JMenuItem itemExit = new JMenuItem(“退出”);
JMenu menuStu = new JMenu(“学生管理”);
JMenuItem itemAdds = new JMenuItem(“增加”);
JMenuItem itemEdits = new JMenuItem(“修改”);
JMenuItem itemDeletes = new JMenuItem(“删除”);
JMenuItem itemSearch = new JMenuItem(“查询”);
Font t = new Font(“Dialog”,0,12);
JMainClient(){
itemAdds.addActionListener(this);
itemEdits.addActionListener(this);
itemDeletes.addActionListener(this);
itemSearch.addActionListener(this);
itemExit.addActionListener(this);
this.pack();
setTitle(“学生管理信息系统”);
setSize(800,600);
setLocationRelativeTo(null);
menuSystem.setFont(t);
menuSystem.add(itemExit);
menuStu.add(itemAdds);
menuStu.add(itemEdits);
menuStu.add(itemDeletes);
menuStu.add(itemSearch);
mainMenu.add(menuSystem); mainMenu.add(menuStu);
this.setJMenuBar(mainMenu);
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if(obj == itemAdds) {
new JAddStudent().setVisible(true);
}
else if(obj == itemEdits) {
//new JUpdateStudent.setVisible(true);
}
else if(obj == itemDeletes) {
//new JDelStudent().setVisible(true);
}
else if(obj == itemSearch) {
new JQuery().setVisible(true);
}
else if(obj == itemExit) {
this.dispose();
}
}
}
JaddStudent类是添加学生信息的子窗体,它定义了textNumber,textName,textAge,textGrade四个编辑控件作为学生信息的录入和显示窗口,addBtn为按钮控件。该类在构造函数内初始化编辑控件和按钮控件,并为每个控件创建Jpanel面板,最后将Jpanel面板添加到窗体中。
public class JAddStudent extends JFrame implements ActionListener {
JButton addBtn;
JTextField textNumber,textName,textAge,textGrade;
JAddStudent(){
setSize(600,300);
setTitle(“增加学生信息”);
setLocationRelativeTo(null);
textNumber = new JTextField(10);
textName = new JTextField(10);
textAge = new JTextField(10);
textGrade = new JTextField(10);
JLabel labelName = new JLabel(“姓名:”);
JLabel lableNumber = new JLabel(“学号:”);
JLabel labelAge = new JLabel(“年龄:”);
JLabel labelGrade = new JLabel(“年级:”);
setLayout(new GridLayout(3,2));
JPanel panelNumber = new JPanel();
JPanel panelName = new JPanel();
JPanel panelAge = new JPanel();
JPanel panelGrade = new JPanel();
JPanel panelButton = new JPanel();
panelNumber.add(lableNumber);
panelNumber.add(textNumber);
panelName.add(labelName);
panelName.add(textName);
panelAge.add(labelAge);
panelAge.add(textAge);
panelGrade.add(labelGrade);
panelGrade.add(textGrade);
addBtn = new JButton(“增加学生信息”);
addBtn.addActionListener(this);
panelButton.add(addBtn);
add(panelNumber);
add(panelName);
add(panelAge);
add(panelGrade);
add(panelButton);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if(obj == addBtn) {
String name = this.textName.getText();
String num = this.textNumber.getText();
String age = this.textAge.getText();
String grade = this.textGrade.getText();
DataMamager dataMamager = new DataMamager();
try {
dataMamager.OpenConn();
String sql = “select * from stinfo where number=” + num + “”;
dataMamager.rs = dataMamager.executeQuery(sql);
if(dataMamager.rs.next()) {
JOptionPane.showMessageDialog(null, “该学生信息已经存在”);
return;
}
sql = “insert into stinfo(number,name,age,grade) values(“+num+”,”+name+”,”+age+”,”+grade+”)”;
System.out.println(sql);
dataMamager.execute(sql);
JOptionPane.showMessageDialog(null, “学生信息添加成功”);
}catch(Exception e1) {
System.err.println(e1.getMessage());
}
dataMamager.closeStmt();
dataMamager.closeConn();
}
}
}
actionPerformed(ActionEvent e)方法为窗体事件处理方法,当老师单击addBtn按钮控件时,该方法被调用。该方法获取四个编辑控件的值,并实例化DataMamager对象,首先执行SQL查询语句,查询待添加的学生学号是否已在数据库中,若该学号没在数据库内,执行SQL插入语句,将学习信息插入到数据库。
请同学们参照JaddStudent类,编写JupdateStudent和JdelStudent类代码。
运行程序
编写一个测试类,启动系统。
public class test {
public static void main(String[] args) {
JMainClient client = new JMainClient();
client.setVisible(true);
client.setResizable(false);
client.setLocationRelativeTo(null);
}
}
声明:本文部分素材转载自互联网,如有侵权立即删除 。
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
丞旭猿论坛
暂无评论内容