文章目录
- 开发小型的学生信息管理系统
- 实验要求
- 1.学生信息管理系统主界面
- 2.数据库university中表student的示例
- 3.核心代码展示
- ==**主界面StudentManagement**==
- ==**录入界面Add**==
- ==**查询界面Query**==
- ==**更新界面 Update**==
- ==**删除界面 Update**==
- ==**加载JDBC**==
- 4.界面展示
-
开发小型的学生信息管理系统
实验要求
- 需求分析(系统的功能设计)
- 绘制E-R图
- 创建数据库
- 用JAVA开发该系统的前端
- 连接数据库
- 要求该系统有对数据库的增、删、改、查的功能。
1.学生信息管理系统主界面
2.数据库university中表student的示例
3.核心代码展示
主界面StudentManagement
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class StudentManagement extends Jframe implements ActionListener{
JMenuBar bar = null;
JMenu menu1,menu2,menu3,menu4,menu5; // 菜单栏
JMenuItem item1,item2,item3,item4,item5; // 项目栏
Add zengjia; //增加页面
Query chaxun; //查询页面
Update gengxin; //更新页面
Delete shanchu; //删除页面
JDBCTest ceshi;//JDBC
StudentManagement(){
super("学 生 信 息 管 理 系 统");
zengjia = new Add();
chaxun = new Query(); //创建各自的对象
gengxin = new Update(); //创建各自的对象
shanchu = new Delete(); //创建各自的对象
ceshi = new JDBCTest(); //创建各自的对象
bar = new JMenuBar();
menu1 = new JMenu("信息录入");
menu2 = new JMenu("信息查询");
menu3 = new JMenu("信息更新");
menu4 = new JMenu("信息删除");
menu5 = new JMenu("退出系统");
item1 = new JMenuItem("录 入");
item2 = new JMenuItem("查 询");
item3 = new JMenuItem("更 新");
item4 = new JMenuItem("删 除");
item5 = new JMenuItem("退 出");
menu1.add(item1);
menu2.add(item2);
menu3.add(item3);
menu4.add(item4);//定义
menu5.add(item5);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);//定义
bar.add(menu4);
bar.add(menu5);
setJMenuBar(bar);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
JLabel label = new JLabel("学生信息管理系统",JLabel.CENTER);
JLabel labe2 = new JLabel("欢迎登陆",JLabel.LEFT);
label.setFont(new Font("学生信息管理系统",0,35));
labe2.setFont(new Font("欢迎登陆",0,18));
String s = " ";
Font f = new Font(s,Font.BOLD,60);
label.setBackground(new Color(200,200,200));
label.setForeground(new Color(30,144,255));
add(label,"Center");
setVisible(true);
add(labe2,"South");
setVisible(true);
setSize(400,400);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == item1){
this.getContentPane().removeAll();
this.getContentPane().add(zengjia,"Center");
this.getContentPane().repaint();
this.getContentPane().validate();
}
if(e.getSource() == item2){
this.getContentPane().removeAll();
this.getContentPane().add(chaxun,"Center");
this.getContentPane().repaint();
this.getContentPane().validate();
}
if(e.getSource() == item3){
this.getContentPane().removeAll();
this.getContentPane().add(gengxin,"Center");
this.getContentPane().repaint();
this.getContentPane().validate();
}
if(e.getSource() == item4){
this.getContentPane().removeAll();
this.getContentPane().add(shanchu,"Center");
this.getContentPane().repaint();
this.getContentPane().validate();
}
if(e.getSource() == item5){
System.exit(0);
}
}
public static void main(String args[]){
StudentManagement stuM = new StudentManagement();
stuM.setVisible(true);
stuM.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
录入界面Add
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Add extends JPanel implements ActionListener{
Connection con; //与特定数据库的连接
Statement sql; //用于执行静态 SQL 语句并返回它所生成结果的对象。
JButton b1,b2;
JTextField tf1,tf2,tf3,tf4,tf5,tf6;
Box baseBox,bv1,bv2;
Add(){
try{ //异常处理
Class.forName("com.mysql.cj.jdbc.Driver"); //通过 Class.forName为数据库管理系统加载一个JDBC驱动程序。
}
catch(ClassNotFoundException e){} //如果加载驱动失败 控制台抛出异常
try{ //如果加载驱动成功, 调用驱动连接特定数据库
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
//数据库的地址
sql=con.createStatement(); //调取con成员方法获取Statement对象
}
catch(SQLException ee){}
//设置面板布局 为边框布局
setLayout(new BorderLayout());
JPanel p1=new JPanel();
JPanel p2=new JPanel();//创建两个布局对象
tf1=new JTextField(16);
tf2=new JTextField(16);
tf3=new JTextField(16);
tf4=new JTextField(16);
tf5=new JTextField(16);
tf6=new JTextField(16);
b1=new JButton("录入");
b2=new JButton("重置");
b1.setBackground(new Color(240,230,140));
b1.setForeground(new Color(30,144,255));
b2.setForeground(new Color(199,21,133));
b2.setBackground(new Color(255,215,0));
b1.addActionListener(this);
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
bv1=Box.createVerticalBox();
bv1.add(new JLabel("学号"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("性别"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("地址"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("电话"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("专业"));
bv1.add(Box.createVerticalStrut(8));
//创建第二个box
bv2=Box.createVerticalBox();
bv2.add(tf1);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);//将box写入p2模块
add(p1,"South");
add(p2,"Center");
setSize(400,400);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
try{ insert();}
catch(SQLException ee){}
JOptionPane.showMessageDialog(this,"数据已入库!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
else if(e.getSource()==b2){
tf1.setText(" ");
tf2.setText(" ");
tf3.setText(" ");
tf4.setText(" ");
tf5.setText(" ");
tf6.setText(" ");
}
}
public void insert() throws SQLException{
String s1="'"+tf1.getText().trim()+"'";
String s2="'"+tf2.getText().trim()+"'";
String s3="'"+tf3.getText().trim()+"'";
String s4="'"+tf4.getText().trim()+"'";
String s5="'"+tf5.getText().trim()+"'";
String s6="'"+tf6.getText().trim()+"'";
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");//建立链接
String temp="INSERT INTO student VALUES ("+s1+","+s2+","+s3+","+s4+","+s5+","+s6+")";
sql.executeUpdate(temp);
con.close();//关闭结果集
}
}
查询界面Query
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Query extends JPanel implements ActionListener{
Connection con;
Statement sql;
JTextField t1,t2,t3,t4,t5,t6;//返回内容
JButton b;
Box baseBox,bv1,bv2; //box
int flag = 0;
Query(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch(ClassNotFoundException e){}
try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
sql = con.createStatement();
}
catch(SQLException ee){}
setLayout (new BorderLayout());
b = new JButton("查询");
b.setBackground(Color.orange);
b.addActionListener(this);
t1 = new JTextField(8);
t2 = new JTextField(16);
t3 = new JTextField(16);
t4= new JTextField(16);
t5 = new JTextField(16);
t6 = new JTextField(16);
t2.setEditable(false);
t3.setEditable(false);
t4.setEditable(false);
t5.setEditable(false);
t6.setEditable(false);
JPanel p1 = new JPanel(),p2 = new JPanel();
p1.add(new JLabel("请输入要查询学生的学号:"));
p1.add(t1);
p1.add(b);
bv1 = Box.createVerticalBox();
bv1.add(new JLabel("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("性别"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("地址"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("电话"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("专业"));
bv1.add(Box.createVerticalStrut(8));
bv2 = Box.createVerticalBox();
bv2.add(t2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t6);
bv2.add(Box.createVerticalStrut(8));
baseBox = Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
add(p1,"North");
add(p2,"Center");
setSize(400,400);
setBackground(Color.white);
}
public void actionPerformed(ActionEvent e){
flag = 0;
try{query();}
catch(SQLException ee){}
}
public void query() throws SQLException{
String num,name,gender,address,phone,major;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");//建立
num = t1.getText().trim();
ResultSet rs = sql.executeQuery("SELECT* FROM student WHERe id = '"+ num +"'"); //结果集
if(rs.next()){//检测有输入
name = rs.getString("name");
gender = rs.getString("gender");
address = rs.getString("address");
phone = rs.getString("phone");
major = rs.getString("major");
t2.setText(name);
t3.setText(gender);
t4.setText(address);
t5.setText(phone);
t6.setText(major);
flag = 1;
}else{
JOptionPane.showMessageDialog(this,"没有该学生!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
con.close();//关闭结果集
if(flag == 0){t1.setText("没有该学生");}
}
}
更新界面 Update
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.*;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Update extends JPanel implements ActionListener{
Connection con;
Statement sql;
JTextField t1,t2,t3,t4,t5,t6;
JButton b1,b2,b3;
Box baseBox,bv1,bv2;
Update(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch(ClassNotFoundException e){}
try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
sql = con.createStatement();
}
catch(SQLException ee){}
//设置面板布局 为边框布局
setLayout(new BorderLayout());
JPanel p1=new JPanel(); //顶部
JPanel p2=new JPanel(); //中间内容
JPanel p3=new JPanel(); //底部按钮
t1=new JTextField(8);
t2=new JTextField(16);
t3=new JTextField(16);
t4=new JTextField(16); //创建框
t5=new JTextField(16);
t6=new JTextField(16);
b1=new JButton("查找");
b1.setBackground(Color.green); //查找功能--属于p1模块
b2=new JButton("更新");
b2.setBackground(Color.orange);
b3=new JButton("取消"); //更新取消功能--属于p3模块
b3.setBackground(Color.PINK);
b1.addActionListener(this);
b2.addActionListener(this); //插入
b3.addActionListener(this);
p1.add(new JLabel("请输入要修改学生的学号:"));
p1.add(t1);
p1.add(b1); //p1模块内容
p3.add(b2);
p3.add(b3); //p3模块内容写入
//创建box bv1---提示文字
bv1=Box.createVerticalBox();
bv1.add(new JLabel("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("性别"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("地址"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("电话"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new JLabel("专业"));
bv1.add(Box.createVerticalStrut(8));
//创建box bv2---填写信息交给T
bv2=Box.createVerticalBox();
bv2.add(t2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);//中间内容---p2模块
add(p1,"North");
add(p2,"Center"); //设置位置顶部中间底部三部分显示
add(p3,"South");
setSize(400,400); //设置大小
setBackground(Color.pink); //颜色
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1) {
try {
String num,name,gender,address,phone,major;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456"); //建立
num=t1.getText().trim();
ResultSet rs = sql.executeQuery("SELECT* FROM student WHERe id = '"+ num +"'"); //结果集
if(rs.next()) {
name=rs.getString("name");
gender=rs.getString("gender");
address=rs.getString("address");
phone=rs.getString("phone");
major=rs.getString("major");
t2.setText(name);
t3.setText(gender);
t4.setText(address);
t5.setText(phone);
t6.setText(major);
}else {
JOptionPane.showMessageDialog(this,"没有该学生!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
con.close();
}
catch(SQLException ee) {}
}
if(e.getSource()==b2) {
try {
update();
}catch(SQLException ee) {}
}
if(e.getSource()==b3) {
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
}
}
public void update() throws SQLException{
String s1="'"+t1.getText().trim()+"'";
String s2="'"+t2.getText().trim()+"'";
String s3="'"+t3.getText().trim()+"'";
String s4="'"+t4.getText().trim()+"'";
String s5="'"+t5.getText().trim()+"'";
String s6="'"+t6.getText().trim()+"'";
String test ="UPDATE student SET name ="+s2+",gender="+s3+",address="+s4+",phone="+s5+",major="+s6+" WHERe id="+s1;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
sql.executeUpdate(test);
JOptionPane.showMessageDialog(this,"修改成功!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
con.close();//关闭结果集
}
}
删除界面 Update
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Delete extends JPanel implements ActionListener{
Connection con;
Statement sql;
JTextField t1,t2,t3,t4,t5,t6;
JButton b,b1,b2;
Box baseBox,bv1,bv2;
Delete(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");//加载jdbc驱动
}
catch(ClassNotFoundException e){}
try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
sql = con.createStatement();
}
catch(SQLException ee){
}
//设置面板布局 为边框布局
setLayout(new BorderLayout());
JPanel p1=new JPanel();
JPanel p2=new JPanel();
t1=new JTextField(8);
t2=new JTextField(16);
t3=new JTextField(16);
t4=new JTextField(16);
t5=new JTextField(16);
t6=new JTextField(16);
b=new JButton("删除");
b.addActionListener(this);
p1.add(new JLabel("请输入要删除的学生学号:"));
p1.add(t1);
p1.add(b);
add(p1,"North");
add(p2,"Center");
setSize(400,400);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==t1) {
try {
delete();
}catch(SQLException ee) {}
}else if(e.getSource()==b) {
try {
delete();
}catch(SQLException ee) {}
int n=JOptionPane.show/confirm/iDialog(this, "确定要删除该学生的信息?","确定",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION) {
try {
String s1=""+t1.getText().trim()+"";
String test="DELETE FROM student WHERe id="+s1;
System.out.println(test);
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
sql.executeUpdate(test);
JOptionPane.showMessageDialog(this,"已经删除成功!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
con.close();
}catch(SQLException ee) {}
}else if(n==JOptionPane.NO_OPTION) {
}
}
}
public void delete() throws SQLException{
String num,name,gender,address,phone,major;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/university?characterEncoding=utf8","root","123456");
num=t1.getText().trim();
ResultSet rs = sql.executeQuery("SELECT* FROM student WHERe id = '"+ num +"'");
if(rs.next()) {
num=rs.getString("id");
name=rs.getString("name");
gender=rs.getString("gender");
address=rs.getString("address");
phone=rs.getString("phone");
major=rs.getString("major");
if(num.equals(t1.getText().trim())) {
t2.setText(name);
t3.setText(gender);
t4.setText(address);
t5.setText(phone);
t6.setText(major);
}
}else {
JOptionPane.showMessageDialog(this,"没有该学生!","提示对话框",JOptionPane.INFORMATION_MESSAGE);
}
con.close();//关闭结果集
}
}
加载JDBC
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
public class JDBCTest {
public static Connection getConnection() {
String driver = "com.mysql.cj.jdbc.Driver"; // 获取mysql数据库的驱动类
String url = "jdbc:mysql://localhost:3306/university"; // 连接数据库
String name = "root";// 连接mysql的用户名
String pwd = "123456";// 连接mysql的密码
try {//异常处理
// 1. 加载驱动程序
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, name, pwd);
// 2.打开数据库连接
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
Connection cc = JDBCTest.getConnection();
if (!cc.isClosed())
System.out.println("Succeeded connecting to the Database!");
// 3. 获取statement对象
Statement statement = cc.createStatement();
//查询数据
String sql = "select * from student";
// 4. 执行SQL语句
ResultSet rs = statement.executeQuery(sql);
// 5. 处理结果集
while (rs.next()) {
System.out.println(rs.getString("Sno") + "");
System.out.println(rs.getString("sname") + "");
System.out.println(rs.getString("ssex") + "");
}
}
}
4.界面展示
1.录入界面
2.查询界面
3.更新界面
4.删除界面