文章目录
本文有借鉴别人的代码,但是也是自己理解之后写出来的,文中有注释,并且有个别文件也用红框和黄字体讲解,并且能解决其他文件90%看不懂的问题,如果还是看不懂清留言,源码在文章末尾,附上了全部代码,这个注册系统比较简陋,供大家学习参考。
一、系统简介(思维导图)
**
这是一个简单的思维导图大家将就着看
**不知道思维导图能不能显示出来,可能有点问题,但是复制链接能访问,如果有需要我会补上
二、JDBC模块
1、声明所需
publicstaticfinalString JDBC_DRIVER ="com.mysql.cj.jdbc.Driver";//版本低的可以删除中间得cj.publicstaticfinalString DB_URL ="jdbc:mysql://localhost:3306/java_sims?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//在问号之后的东西都是一些去重连接中可能会报错的地方publicstaticfinalString username ="root";//数据库账号publicstaticfinalString password ="111111";//数据库密码privateConnection connection =null;//初始化conn~privatePreparedStatement pStatement =null;//初始化stat~privateResultSet rSet =null;//初始化结果
以上代码是调用
JDBC
的
jar包
并且设置参数和初始化,版本低一点的
JDBC
包可以删除第一行中间的
.jc
有一点要注意的是 代码中
java_sims
是我的数据库名,还有在
java_sims
之后的可以不写,但是有可能会报错,还是建议写上至于参数是
false
还是
true
看你报错的地方让你设置什么
然后就是数据库账号和密码 记得根据自己的写昂
然后就是一些先行初始化的东西
2、加载驱动
在这里我们能看见一个
conn
这个是调用数据库驱动,用来链接数据库。
3、关闭资源
4、插入记录
5、更新记录
6、删除记录
7、按学号查询学生记录
三、Swing(所有的窗口)
Swing+JDBC 因为这里我有一个文件包含了JDBC和Swing所以先把它拿来讲解
1)先是
JDBC
部分
2)然后就是
Swing
部分
四、接下来就是放源码
我会将源码分
JDBC
和
Swing
放在两个标题下,每个代码上面都有一个标题看表示文件名
在放
JDBC
和
Swing
源码前有两个文件需要构造
Student
packageStudentManager;/**
* 功能: 该文件是声明Studeent所需要的变量并且构造方法
* 作者:Sherry
* 日期:2022-06-02
*/publicclassStudent{privateString name;privateString gender;privateString dob;privateint batch;publicStudent(String name,String gender,String dob,int batch){this.name = name;this.gender = gender;this.dob = dob;this.batch = batch;}publicStringgetName(){return name;}publicStringgetGender(){return gender;}publicStringgetDob(){return dob;}publicintgetBatch(){return batch;}}
User
packageStudentManager;/**
* 功能: 初始化管理员在数据库的username和password
* 作者:Sherry
* 日期:2022-06-06
*/publicclassUser{privateString username;privateString password;publicUser(String username,String password){//在这申明用户的用户名和密码 在登录的时候会使用this.username = username;this.password = password;};//下面的getter和setter系统自动生成publicStringgetUsername(){return username;}publicvoidsetUsername(String username){this.username = username;}publicStringgetPassword(){return password;}publicvoidsetPassword(String password){this.password = password;}}
还有一个文件需要写就是注册文字的监听
Regevent
packageStudentManager;importStudentManager.test.RegisterFrame;importjavax.swing.*;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;/**
* 功能: 鼠标对“注册”文字的监听
* 作者:Sherry
* 日期:2022-06-02
*///其实这里的没有必要当然我不想改了所以就将就用了publicclassRegeventimplementsMouseListener{@OverridepublicvoidmouseClicked(MouseEvent e){//当鼠标左键按下的时候才会触发事件if(e.getButton()==MouseEvent.BUTTON1){JOptionPane.showMessageDialog(null,"hello");Register r =newRegister("用户注册");}}@OverridepublicvoidmousePressed(MouseEvent e){System.out.println("按住经过");}@OverridepublicvoidmouseReleased(MouseEvent e){System.out.println("按住鼠标");}@OverridepublicvoidmouseEntered(MouseEvent e){System.out.println("滑动鼠标");}@OverridepublicvoidmouseExited(MouseEvent e){System.out.println("鼠标退出");}}
JDBC代码
- 下面就是JDBC所属代码
SQLHelp
packageStudentManager;/**
* 功能: 运行SQL语句
* 作者:Sherry
* 日期:2022-06-02
*/importjava.sql.*;publicclassSQLHelp{publicstaticfinalString JDBC_DRIVER ="com.mysql.cj.jdbc.Driver";//版本低的可以删除中间得cj.publicstaticfinalString DB_URL ="jdbc:mysql://localhost:3306/java_sims?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//在问好之后的东西都是一些去重连接中可能会报错的地方publicstaticfinalString username ="root";//数据库账号publicstaticfinalString password ="111111";//数据库密码privateConnection connection =null;//初始化conn~privatePreparedStatement pStatement =null;//初始化stat~privateResultSet rSet =null;//初始化结果// 加载驱动// 静态初始化块(只执行一次)static{try{Class.forName(JDBC_DRIVER);}catch(ClassNotFoundException e){//TODO Auto-generated catch block
e.printStackTrace();//抛出异常}}// 链接数据库publicvoidconnectDB(){try{
connection =DriverManager.getConnection(DB_URL,username,password);System.out.println("数据库链接成功");}catch(SQLException e){System.out.println("数据库链接失败");
e.printStackTrace();//链接失败抛出异常}}// 关闭资源publicvoidclose(){if(rSet !=null){try{
rSet.close();}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(pStatement !=null){try{
pStatement.close();}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}if(connection !=null){try{
connection.close();}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}}}//申明变量并且说明是SQL模块publicvoidaddStudent(int id,String nameString,String genderString,String dobString,int batch)throwsSQLException{// try finally 无论是否抛出异常都将执行 finally 中的语句try{// 先链接到数据库connectDB();// sql 语句// 静态 sql 语句需要进行字符串拼接// 动态 sql 语句String addsql ="insert into student values(?, ?, ?, ?, ?)";//添加字段值
pStatement = connection.prepareStatement(addsql);
pStatement.setInt(1, id);
pStatement.setString(2, nameString);
pStatement.setString(3, genderString);
pStatement.setString(4, dobString);
pStatement.setInt(5, batch);
pStatement.executeUpdate();}finally{close();}}publicvoidaddManeger(String username,String password)throwsSQLException{// try finally 无论是否抛出异常都将执行 finally 中的语句try{// 先链接到数据库connectDB();// sql 语句// 静态 sql 语句需要进行字符串拼接// 动态 sql 语句String addsql ="insert into m_user values(?, ?)";//添加字段值
pStatement = connection.prepareStatement(addsql);
pStatement.setString(1, username);
pStatement.setString(2, password);
pStatement.executeUpdate();}finally{close();}}publicvoidchangeStudent(int id,String nameString,String genderString,String dobString,String batch)throwsSQLException{try{connectDB();String changesql ="update student set name = ?, gender = ?, dob = ?, batch = ? where id = ?";
pStatement = connection.prepareStatement(changesql);
pStatement.setString(1, nameString);
pStatement.setString(2, genderString);
pStatement.setString(3, dobString);
pStatement.setString(4, batch);
pStatement.setInt(5, id);
pStatement.executeUpdate();}finally{close();}}publicvoiddeletestudent(int id)throwsSQLException{try{connectDB();String deleteString ="select * from student where id = ?";
pStatement = connection.prepareStatement(deleteString);
pStatement.setInt(1, id);
pStatement.execute();}finally{close();}}publicUseruserpassweord(String username)throwsSQLException{try{connectDB();//调用数据库相关数据String slelectusername ="select * from m_user where username = ?";
pStatement = connection.prepareStatement(slelectusername);
pStatement.setString(1, username);
rSet = pStatement.executeQuery();if(rSet.next()){String usernameString = rSet.getString(1);String passwordString = rSet.getString(2);returnnewUser(usernameString, passwordString);}else{returnnull;}}finally{close();}}publicStudentqueryStudent(int id)throwsSQLException{try{connectDB();String querysql ="select * from student where id = ?";
pStatement = connection.prepareStatement(querysql);
pStatement.setInt(1, id);// 三种方法执行// execute() 都可以使用,返回true或false// executeQuery() 专门用于查询,返回结果集// executeUpdate() 专门用于删除、更新
rSet = pStatement.executeQuery();if(rSet.next()){String nameString = rSet.getString(2);String genderString = rSet.getString(3);String dobString = rSet.getString(4);int batchString = rSet.getInt(5);// 查询到学生信息返回结果集returnnewStudent(nameString, genderString, dobString, batchString);}else{// 没有查询到学生信息,返回nullreturnnull;}}finally{close();}}}
SelectFrame
- 因为JDBC本来是写在一起的这里我单独写了一个文件,我就把它并在JDBC内容里面
packageStudentManager;/**
* 功能:
* 作者:Sherry
* 日期:2022-06-02
*/importjavax.swing.*;importjava.awt.*;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.sql.*;publicclassSelectFrameextendsJFrame{publicstaticfinalString JDBC_DRIVER ="com.mysql.cj.jdbc.Driver";//版本低的可以删除中间得cj.publicstaticfinalString DB_URL ="jdbc:mysql://localhost:3306/java_sims?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//在问好之后的东西都是一些去重连接中可能会报错的地方publicstaticfinalString username ="root";//数据库账号publicstaticfinalString password ="111111";//数据库密码//创建窗口publicSelectFrame()throwsSQLException{// JTable resultarea = new JTable();Connection connection=null;//初始化conn
connection =DriverManager.getConnection(DB_URL,username,password);PreparedStatement preparedStatement=connection.prepareStatement("select *from student");//sql语句编译预处理ResultSet res=preparedStatement.executeQuery();//获取ResultSet结果集
res.last();//游标移动到查询到的数据库数据记录的最后一条int row=res.getRow();//获取记录的总条数
res.beforeFirst();//游标回到第一个记录前的位置String arr[]={"姓名","性别","出生日期","班级"};//定义表格的列名称String comm[][]=newString[row][4];//row行,4列
res.next();//游标回到第一个记录的位置for(int i=0;i<row;i++){
comm[i][0]=res.getString("name");
comm[i][1]=res.getString("sex");
comm[i][2]=res.getString("birthday");
comm[i][3]=res.getString("gender");
res.next();}JTable jTable=newJTable(comm,arr);
jTable.setPreferredScrollableViewportSize(newDimension(800,200));//设置可滚动视图的大小JScrollPane jScrollPane=newJScrollPane(jTable);add(jScrollPane,BorderLayout.CENTER);//将滚动条放到窗体// setDefaultCloseOperation(EXIT_ON_CLOSE);//这个是关闭全部程序setVisible(true);//设置窗体可见validate();//设置组件可见// setLocationRelativeTo(null);this.setBounds(550,400,800,200);pack();//自动调整组建大小使每个组键都能显示
connection.close();//设置程序关闭方式,this.addWindowListener(newWindowAdapter(){@OverridepublicvoidwindowClosing(WindowEvent e){int choice =JOptionPane.showConfirmDialog(null,"关闭该窗口?","学生管理",JOptionPane.YES_NO_OPTION);if(choice ==JOptionPane.YES_OPTION){dispose();}else{try{newSelectFrame();}catch(SQLException ex){
ex.printStackTrace();}}}});}}
Swing
Start
这个文件是启动文件哈,启动程序的时候用这个启动
packageStudentManager;/**
* 功能: 执行程序
* 作者:Sherry
* 日期:2022-06-06
*/publicclassStart{publicstaticvoidmain(String[] args){LoginStar l =newLoginStar();}}
LoginStar
packageStudentManager;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;/**
* 功能: LoginFrame
* 作者:Sherry
* 日期:2022-05-16
*/publicclassLoginStarextendsJFrame{//需要5个标签 一个文本框 一个密码框 一个登录按钮JLabel bgimg;//定义背景标签JLabel username;//定义账号标签JLabel password;//定义密码标签// JLabel title; //定义标题标签JTextField usernametext;//输入账号的文本框JPasswordField passwordtext;//输入密码的文本框JButton login;//登录按钮JLabel register;//注册标签//窗口变量finalint WIDTH =300;//设置顶层框架的宽度finalint HEIGHT =300;//设置顶层框架的高度//定义一个箱子JPanel p;//放图片的容器Regevent regeveent;//定义注册标签的书签监听事件publicLoginStar(){init();setResizable(false);//窗口的大小不可变setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置默认关闭方式validate();//让组件生效setVisible(true);//设置当前窗口是否可以显示}privatevoidinit(){//给容器创建对象
p =newJPanel();
p.setBounds(0,0, WIDTH, HEIGHT);
p.setSize(300,300);
p.setLayout(null);// 设置布局为空//设置当前窗口大小// this.setBounds();Toolkit kit =Toolkit.getDefaultToolkit();//获取对象大小 设置窗口位置Dimension screenSize = kit.getScreenSize();int width = screenSize.width;int height = screenSize.height;int x =(width - WIDTH)/2;int y =(height - HEIGHT)/2;// this.setSize(WIDTH, HEIGHT);// this.setLocation(x, y);//设置位置和窗口大小,以上设置背景设置图标this.setBounds(x, y, WIDTH, HEIGHT);this.setTitle("登录");this.add(p);//主窗口添加 容器 p// //背景图片会导致下面不能直接显示因为层次不对// ImageIcon img = new ImageIcon("src/img/bg.png");//设置背景赌片// bgimg = new JLabel(img);// //设置图片位置// bgimg.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());// p.add(bgimg); //添加图片到容器 p
username =newJLabel("账号");
password =newJLabel("密码");// title = new JLabel("登录");
login =newJButton("登录");
register =newJLabel("注册");// title.setSize(30,30);
usernametext =newJTextField(20);
passwordtext =newJPasswordField(20);
passwordtext.setEchoChar('*');// title.setBounds(10,10,120,25);
passwordtext.setBounds(125,120,120,25);
usernametext.setBounds(125,90,120,25);
password.setBounds(60,120,60,25);
username.setBounds(60,90,60,25);// register.setBounds(0,200,200,30);
login.setBounds(90,180,60,25);
register.setBounds(10,200,100,100);// p.setVisible(true);// p.add(register);
p.add(username);
p.add(password);// p.add(title);
p.add(usernametext);
p.add(passwordtext);
p.add(login);
p.add(register);
login.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){String strusername = usernametext.getText();String strpassword =newString(passwordtext.getPassword());SQLHelp sqlHelp =newSQLHelp();try{User users = sqlHelp.userpassweord(strusername);String username = users.getUsername();String password = users.getPassword();System.out.println(username);System.out.println(password);if(strusername.equals(username)&& strpassword.equals(password)){JOptionPane.showMessageDialog(null,"登录成功","登陆成功提示窗口",JOptionPane.INFORMATION_MESSAGE);// Register r = new Register();// r.setVisible(true);MainFrame main =newMainFrame();
main.setVisible(true);// CommonFrame c = new CommonFrame();}else{JOptionPane.showMessageDialog(null,"登录失败,若没有账号点击左下角注册","登录失败提示窗口",JOptionPane.INFORMATION_MESSAGE);// Register r = new Register();//确定之后自动跳入注册页面// r.setVisible(true);}}catch(SQLException ex){
ex.printStackTrace();}}});// register.addActionListener(new ActionListener() {// @Override// public void actionPerformed(ActionEvent e) {// Register r = new Register();// r.setVisible(true);// }// });allEvent();}//所有处理事件都在这个地方去写voidallEvent(){
regeveent =newRegevent();
register.addMouseListener(regeveent);}}
Register
packageStudentManager;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;/**
* 功能: 注册账号
* 作者:Sherry
* 日期:2022-05-30
*/publicclassRegisterextendsJFrame{JLabel username;JLabel password;JLabel passwordAgain;JTextField usernameFile;JPasswordField passwordFile;JPasswordFieldPasswordFileAgain;// FlowLayout flowLayout;JButton sign;JPanel p;JPanel p1;// private Connection connection = null;//初始化conn~// private PreparedStatement pStatement = null;//初始化stat~// private ResultSet rSet = null;//初始化结果finalint WIDTH =410;finalint HEIGHT =610;// flowLayout = new FlowLayout(FlowLayout.CENTER);publicRegister(){init();//调用init方法setResizable(false);//这是不可变窗口setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置窗口关闭默认关闭方式validate();setVisible(true);//可显示Toolkit kit =Toolkit.getDefaultToolkit();//获取屏幕宽度Dimension screenSize = kit.getScreenSize();//初始化变量int width = screenSize.width;//这边都是设置大小的,当然后面我老师i给我讲的时候发现根本不需要int height = screenSize.height;int x =(width - WIDTH)/2;int y =(height - HEIGHT)/2;setBounds(x, y, WIDTH, HEIGHT);//设置主窗口位置和大小setTitle("账号注册");//设置主窗口的标题}voidinit(){// p.setBorder(BorderFactory.createTitledBorder("基本信息处理"));// p.setPreferredSize(new Dimension(WIDTH, HEIGHT));// p.setLayout(new FlowLayout(FlowLayout.CENTER));// p.setBounds(0,0,WIDTH,HEIGHT);// p.setLayout(null);// p.setOpaque(false);
p =newJPanel();//初始化窗口p
p.setLayout(null);//设置布局为空
p.setVisible(true);//设置可见
p1 =newJPanel();//这里因为不知道什么原因如果不重新声明一个Panle那么我的内容显示不出来// p1.setLayout(null);
p1.setVisible(true);//设置可见
p1.setLayout(null);//设置恐怖剧
p1.setBounds(0,0,400,600);//设置位置和大小
username =newJLabel("账号:");//这里全是定义所需要的元素
password =newJLabel("密码:");
passwordAgain =newJLabel("确认密码:");
usernameFile =newJTextField(20);//这里是设置长度
passwordFile =newJPasswordField(20);//注意TextFile和PasswordFile在后续获取的时候不一样PasswordFileAgain=newJPasswordField(20);
passwordFile.setEchoChar('*');//这个是输入密码的填充PasswordFileAgain.setEchoChar('*');
sign =newJButton("注册");//设置注册按钮//这里开始设置元素位置和大小
sign.setBounds(160,300,100,50);
username.setBounds(130,150,40,20);
password.setBounds(130,200,40,20);
passwordAgain.setBounds(130,250,120,20);
usernameFile.setBounds(200,150,100,20);
passwordFile.setBounds(200,200,100,20);PasswordFileAgain.setBounds(200,250,100,20);//将元素逐层添加
p1.add(username);
p1.add(password);
p1.add(passwordAgain);
p1.add(usernameFile);
p1.add(passwordFile);
p1.add(PasswordFileAgain);
p1.add(sign);
p.add(p1);this.add(p);//这里我们为注册按钮添加了监听事件
sign.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){String strusername = usernameFile.getText();//获取usernameFile的内容String strpassword =newString(passwordFile.getPassword());//在这里我们获取usernameFile和passwordFile方式就不一样了,看好String strpasswordAgain =newString(PasswordFileAgain.getPassword());SQLHelp sqlHelp =newSQLHelp();//这里我们调用SQLHelp文件 我在那里写了注册所需的模块try{User users = sqlHelp.userpassweord(strusername);//这里我们先接收看看能不能接收到数据if(users ==null){//没数据说明没有账户可以注册if(strpassword.equals(strpasswordAgain)){System.out.println("注册成功");//判断是否进入String username = strusername;//当然这里也可以不用在初始化一次,因为上面可以直接使用String password = strpassword;
sqlHelp.addManeger(username,password);//这里就是向SQLHelp文件里卖弄的addManager模块传入数据JOptionPane.showMessageDialog(null,"注册成功","点击确定后返回登录界面",JOptionPane.INFORMATION_MESSAGE);//这里是两次密码不同}else{System.out.println("注册失败");JOptionPane.showMessageDialog(null,"注册失败,请检查两次密码是否相同","登录失败提示窗口",JOptionPane.INFORMATION_MESSAGE);}//这里是账号重复}else{System.out.println("已有账号无法注册");//控制台提示信息JOptionPane.showMessageDialog(null,"注册失败了,已拥有该账号,请检查密码是否输入错误,目前不支持找回密码","账号已有窗口",JOptionPane.INFORMATION_MESSAGE);// dispose();}}catch(SQLException ex){
ex.printStackTrace();}}});}//这里是单独测试Register文件的时候用的publicstaticvoidmain(String[] args){newRegister();}}
MainFrame
packageStudentManager;importjavax.swing.*;importjavax.swing.border.EmptyBorder;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassMainFrameextendsJFrame{privateJPanel contentPane;// // 有了main函数才可以单独运行 取消注释可以单独测试该文件// public static void main(String[] args) {// EventQueue.invokeLater(new Runnable() {// public void run() {// try {// MainFrame frame = new MainFrame();// frame.setVisible(true);// } catch (Exception e) {// e.printStackTrace();// }// }// });// }publicMainFrame(){// 设置大小不可改变setResizable(false);// 设置标题setTitle("学生管理系统");// 由于这是主页面,所有当主页面关闭的时候,程序就直接退出setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// setBounds(int x,int y,int width,int height);// x 和 y 是窗口打开时的位置// width 和 height 是窗口打宽度和高度setBounds(100,100,450,500);// setLocationRelativeTo(c)// 设置窗口相对于 c 的位置,当 c 为空或者 null 时,默认为是相对于屏幕中央setLocationRelativeTo(null);// 实例化一个 pane
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(null);JLabel lblNewLabel =newJLabel("学生管理系统");
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(newFont("宋体",Font.BOLD,25));
lblNewLabel.setBounds(140,10,163,44);
contentPane.add(lblNewLabel);JButton addButton =newJButton("添加学生");// addActionListener 注册监听器
addButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){// 设置新窗口可见newAddFrame().setVisible(true);}});
addButton.setFont(newFont("宋体",Font.PLAIN,18));
addButton.setBounds(167,64,114,37);
contentPane.add(addButton);JButton changeButton =newJButton("修改信息");
changeButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){// 同上newChangeFrame().setVisible(true);}});
changeButton.setFont(newFont("宋体",Font.PLAIN,18));
changeButton.setBounds(167,121,114,37);
contentPane.add(changeButton);JButton deleteButton =newJButton("删除学生");
deleteButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){// 同上newDeleteFrame().setVisible(true);}});
deleteButton.setFont(newFont("宋体",Font.PLAIN,18));
deleteButton.setBounds(167,180,114,37);
contentPane.add(deleteButton);JButton queryButton =newJButton("查询信息");
queryButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){// 同上newQueryFrame().setVisible(true);}});
queryButton.setFont(newFont("宋体",Font.PLAIN,18));
queryButton.setBounds(167,240,114,37);
contentPane.add(queryButton);JButton selectButton =newJButton("查询所有学生记录");
selectButton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvent e){try{SelectFrame s =newSelectFrame();}catch(SQLException ex){
ex.printStackTrace();}}});
selectButton.setBounds(134,300,180,37);
selectButton.setFont(newFont("宋体",Font.PLAIN,18));
contentPane.add(selectButton);}}
QueryFrame
packageStudentManager;/**
* 功能: 查询窗口
* 作者:Sherry
* 日期:2022-06-02
*/importjavax.swing.*;importjavax.swing.border.EmptyBorder;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassQueryFrameextendsJFrame{privateJPanel contentPane;privateJTextField idField;privateJTextField nameField;privateJTextField genderField;privateJTextField dobField;privateJTextField batchField;privateJTextField searchField;publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{QueryFrame frame =newQueryFrame();
frame.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});}publicQueryFrame(){setResizable(false);setTitle("查询信息");setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100,100,450,470);setLocationRelativeTo(null);
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(newBoxLayout(contentPane,BoxLayout.Y_AXIS));JPanel panel =newJPanel();
contentPane.add(panel);
panel.setLayout(null);JLabel lblNewLabel =newJLabel("请输入要查找的学生的学号:");
lblNewLabel.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel.setBounds(71,0,208,29);
panel.add(lblNewLabel);JLabel lblNewLabel_1 =newJLabel("学号:");
lblNewLabel_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1.setBounds(71,105,50,30);
panel.add(lblNewLabel_1);
idField =newJTextField();
idField.setEditable(false);
idField.setBounds(143,99,240,45);
panel.add(idField);
idField.setColumns(10);JLabel lblNewLabel_1_1 =newJLabel("姓名:");
lblNewLabel_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1.setBounds(71,160,50,30);
panel.add(lblNewLabel_1_1);
nameField =newJTextField();
nameField.setColumns(10);
nameField.setBounds(143,154,240,45);
panel.add(nameField);JLabel lblNewLabel_1_1_1 =newJLabel("性别:");
lblNewLabel_1_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1.setBounds(71,215,50,30);
panel.add(lblNewLabel_1_1_1);
genderField =newJTextField();
genderField.setColumns(10);
genderField.setBounds(143,209,240,45);
panel.add(genderField);JLabel lblNewLabel_1_1_1_1 =newJLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1_1.setBounds(41,270,80,30);
panel.add(lblNewLabel_1_1_1_1);
dobField =newJTextField();
dobField.setColumns(10);
dobField.setBounds(143,264,240,45);
panel.add(dobField);JLabel lblNewLabel_1_1_1_2 =newJLabel("班级:");
lblNewLabel_1_1_1_2.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1_2.setBounds(71,325,50,30);
panel.add(lblNewLabel_1_1_1_2);
batchField =newJTextField();
batchField.setColumns(10);
batchField.setBounds(143,319,240,45);
panel.add(batchField);
searchField =newJTextField();
searchField.setBounds(71,39,208,45);
panel.add(searchField);
searchField.setColumns(10);JButton searchButton =newJButton("查找");
searchButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent arg0){// 1.读出要查询的学生输入的id// 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据// 转化一下数据类型int id =Integer.parseInt(searchField.getText());// 2.执行JDBC语句try{SQLHelp sqlHelp =newSQLHelp();Student student = sqlHelp.queryStudent(id);// 3.将查询结果填到文本框中// 前提是学生存在if(student !=null){
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());//性别
dobField.setText(student.getDob());//出生日期
batchField.setText(String.valueOf(student.getBatch()));//班级}else{// 此时学生不存在,提示用户,不存在该学生JOptionPane.showMessageDialog(QueryFrame.this,"无此学生");}}catch(SQLException e){
e.printStackTrace();}}});
searchButton.setFont(newFont("宋体",Font.PLAIN,18));
searchButton.setBounds(289,42,97,39);
panel.add(searchButton);}}
DeleteFrame
packageStudentManager;/**
* 功能: 查询窗口
* 作者:Sherry
* 日期:2022-06-02
*/importjavax.swing.*;importjavax.swing.border.EmptyBorder;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassQueryFrameextendsJFrame{privateJPanel contentPane;privateJTextField idField;privateJTextField nameField;privateJTextField genderField;privateJTextField dobField;privateJTextField batchField;privateJTextField searchField;publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{QueryFrame frame =newQueryFrame();
frame.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});}publicQueryFrame(){setResizable(false);setTitle("查询信息");setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100,100,450,470);setLocationRelativeTo(null);
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(newBoxLayout(contentPane,BoxLayout.Y_AXIS));JPanel panel =newJPanel();
contentPane.add(panel);
panel.setLayout(null);JLabel lblNewLabel =newJLabel("请输入要查找的学生的学号:");
lblNewLabel.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel.setBounds(71,0,208,29);
panel.add(lblNewLabel);JLabel lblNewLabel_1 =newJLabel("学号:");
lblNewLabel_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1.setBounds(71,105,50,30);
panel.add(lblNewLabel_1);
idField =newJTextField();
idField.setEditable(false);
idField.setBounds(143,99,240,45);
panel.add(idField);
idField.setColumns(10);JLabel lblNewLabel_1_1 =newJLabel("姓名:");
lblNewLabel_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1.setBounds(71,160,50,30);
panel.add(lblNewLabel_1_1);
nameField =newJTextField();
nameField.setColumns(10);
nameField.setBounds(143,154,240,45);
panel.add(nameField);JLabel lblNewLabel_1_1_1 =newJLabel("性别:");
lblNewLabel_1_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1.setBounds(71,215,50,30);
panel.add(lblNewLabel_1_1_1);
genderField =newJTextField();
genderField.setColumns(10);
genderField.setBounds(143,209,240,45);
panel.add(genderField);JLabel lblNewLabel_1_1_1_1 =newJLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1_1.setBounds(41,270,80,30);
panel.add(lblNewLabel_1_1_1_1);
dobField =newJTextField();
dobField.setColumns(10);
dobField.setBounds(143,264,240,45);
panel.add(dobField);JLabel lblNewLabel_1_1_1_2 =newJLabel("班级:");
lblNewLabel_1_1_1_2.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1_2.setBounds(71,325,50,30);
panel.add(lblNewLabel_1_1_1_2);
batchField =newJTextField();
batchField.setColumns(10);
batchField.setBounds(143,319,240,45);
panel.add(batchField);
searchField =newJTextField();
searchField.setBounds(71,39,208,45);
panel.add(searchField);
searchField.setColumns(10);JButton searchButton =newJButton("查找");
searchButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent arg0){// 1.读出要查询的学生输入的id// 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据// 转化一下数据类型int id =Integer.parseInt(searchField.getText());// 2.执行JDBC语句try{SQLHelp sqlHelp =newSQLHelp();Student student = sqlHelp.queryStudent(id);// 3.将查询结果填到文本框中// 前提是学生存在if(student !=null){
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());//性别
dobField.setText(student.getDob());//出生日期
batchField.setText(String.valueOf(student.getBatch()));//班级}else{// 此时学生不存在,提示用户,不存在该学生JOptionPane.showMessageDialog(QueryFrame.this,"无此学生");}}catch(SQLException e){
e.printStackTrace();}}});
searchButton.setFont(newFont("宋体",Font.PLAIN,18));
searchButton.setBounds(289,42,97,39);
panel.add(searchButton);}}
ChangeFrame
packageStudentManager;/**
* 功能:
* 作者:Sherry
* 日期:2022-06-02
*/importjavax.swing.*;importjavax.swing.border.EmptyBorder;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassChangeFrameextendsJFrame{privateJPanel contentPane;privateJTextField idField;privateJTextField nameField;privateJTextField genderField;privateJTextField dobField;privateJTextField batchField;privateJTextField searchField;/**
* Launch the application.
*/publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{ChangeFrame frame =newChangeFrame();
frame.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});}/**
* Create the frame.
*/publicChangeFrame(){setResizable(false);setTitle("修改信息");setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100,100,450,470);setLocationRelativeTo(null);
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(newBoxLayout(contentPane,BoxLayout.Y_AXIS));JPanel panel =newJPanel();
contentPane.add(panel);
panel.setLayout(null);JLabel lblNewLabel =newJLabel("请输入要修改的学生的学号:");
lblNewLabel.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel.setBounds(71,0,208,29);
panel.add(lblNewLabel);JLabel lblNewLabel_1 =newJLabel("学号:");
lblNewLabel_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1.setBounds(71,105,50,30);
panel.add(lblNewLabel_1);
idField =newJTextField();
idField.setEditable(false);
idField.setBounds(143,99,240,45);
panel.add(idField);
idField.setColumns(10);JLabel lblNewLabel_1_1 =newJLabel("姓名:");
lblNewLabel_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1.setBounds(71,160,50,30);
panel.add(lblNewLabel_1_1);
nameField =newJTextField();
nameField.setColumns(10);
nameField.setBounds(143,154,240,45);
panel.add(nameField);JLabel lblNewLabel_1_1_1 =newJLabel("性别:");
lblNewLabel_1_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1.setBounds(71,215,50,30);
panel.add(lblNewLabel_1_1_1);
genderField =newJTextField();
genderField.setColumns(10);
genderField.setBounds(143,209,240,45);
panel.add(genderField);JLabel lblNewLabel_1_1_1_1 =newJLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1_1.setBounds(41,270,80,30);
panel.add(lblNewLabel_1_1_1_1);
dobField =newJTextField();
dobField.setColumns(10);
dobField.setBounds(143,264,240,45);
panel.add(dobField);JLabel lblNewLabel_1_1_1_2 =newJLabel("班级:");
lblNewLabel_1_1_1_2.setFont(newFont("宋体",Font.PLAIN,16));
lblNewLabel_1_1_1_2.setBounds(71,325,50,30);
panel.add(lblNewLabel_1_1_1_2);
batchField =newJTextField();
batchField.setColumns(10);
batchField.setBounds(143,319,240,45);
panel.add(batchField);JButton changeButton =newJButton("修改");
changeButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent arg0){int id =Integer.parseInt(searchField.getText());String nameString = nameField.getText();String gendeString = genderField.getText();String dobString = dobField.getText();String batch = batchField.getText();SQLHelp sqlHelp =newSQLHelp();try{
sqlHelp.changeStudent(id, nameString, gendeString, dobString, batch);JOptionPane.showMessageDialog(ChangeFrame.this,"修改成功!");}catch(SQLException e){JOptionPane.showMessageDialog(ChangeFrame.this,"修改失败!");
e.printStackTrace();}}});
changeButton.setFont(newFont("宋体",Font.PLAIN,18));
changeButton.setBounds(182,389,97,33);
panel.add(changeButton);
searchField =newJTextField();
searchField.setBounds(71,39,208,45);
panel.add(searchField);
searchField.setColumns(10);JButton searchButton =newJButton("查找");
searchButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){int id =Integer.parseInt(searchField.getText());try{SQLHelp sqlHelp =newSQLHelp();Student student = sqlHelp.queryStudent(id);if(student !=null){// id 是直接读入的,所以就不需要从数据库中读出了
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());
dobField.setText(student.getDob());
batchField.setText(String.valueOf(student.getBatch()));}else{JOptionPane.showMessageDialog(ChangeFrame.this,"无此用户");}}catch(SQLException e1){
e1.printStackTrace();}}});
searchButton.setFont(newFont("宋体",Font.PLAIN,18));
searchButton.setBounds(289,42,97,39);
panel.add(searchButton);}}
AddFrame
packageStudentManager;/**
* 功能:
* 作者:Sherry
* 日期:2022-06-02
*/importjavax.swing.*;importjavax.swing.border.EmptyBorder;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.SQLException;publicclassAddFrameextendsJFrame{privateJPanel contentPane;privateJTextField idField;privateJTextField nameField;privateJTextField genderField;privateJTextField dobField;privateJTextField batchField;/**
* Launch the application.
*/publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{AddFrame frame =newAddFrame();
frame.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});}/**
* Create the frame.
*/publicAddFrame(){// 和前面类似setResizable(false);setTitle("添加学生");// 这个地方,添加学生只是这个程序的一部分,所以当关闭这部分的时候,程序不直接退出,只是关闭该部分程序setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100,100,450,470);setLocationRelativeTo(null);
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(newBoxLayout(contentPane,BoxLayout.Y_AXIS));JPanel panel =newJPanel();
contentPane.add(panel);
panel.setLayout(null);JLabelTitleLabel=newJLabel("请输入新学生的信息:");TitleLabel.setFont(newFont("宋体",Font.BOLD,20));TitleLabel.setBounds(71,34,220,45);
panel.add(TitleLabel);JLabel idLabel =newJLabel("学号:");
idLabel.setFont(newFont("宋体",Font.PLAIN,16));
idLabel.setBounds(71,105,50,30);
panel.add(idLabel);
idField =newJTextField();
idField.setBounds(143,99,240,45);
panel.add(idField);
idField.setColumns(10);JLabel nameLabel =newJLabel("姓名:");
nameLabel.setFont(newFont("宋体",Font.PLAIN,16));
nameLabel.setBounds(71,160,50,30);
panel.add(nameLabel);
nameField =newJTextField();
nameField.setColumns(10);
nameField.setBounds(143,154,240,45);
panel.add(nameField);JLabel genderLabel =newJLabel("性别:");
genderLabel.setFont(newFont("宋体",Font.PLAIN,16));
genderLabel.setBounds(71,215,50,30);
panel.add(genderLabel);
genderField =newJTextField();
genderField.setColumns(10);
genderField.setBounds(143,209,240,45);
panel.add(genderField);JLabel dobLabel =newJLabel("出生日期:");
dobLabel.setFont(newFont("宋体",Font.PLAIN,16));
dobLabel.setBounds(41,270,80,30);
panel.add(dobLabel);
dobField =newJTextField();
dobField.setColumns(10);
dobField.setBounds(143,264,240,45);
panel.add(dobField);JLabel batchLabel =newJLabel("班级:");
batchLabel.setFont(newFont("宋体",Font.PLAIN,16));
batchLabel.setBounds(71,325,50,30);
panel.add(batchLabel);
batchField =newJTextField();
batchField.setColumns(10);
batchField.setBounds(143,319,240,45);
panel.add(batchField);JButton addButton =newJButton("添加");// 添加鼠标监听事件
addButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){// 先取出数据// getText() 返回字符串类型的// Integer.parseInt 数据类型转换int id =Integer.parseInt(idField.getText());String nameString = nameField.getText();String genderString = genderField.getText();String dobfieldString = dobField.getText();int batch =Integer.parseInt(batchField.getText());// 输出一下学生的信息,方便修改System.out.println(id +"\t"+ nameString +"\t"+ genderString +"\t"+ dobfieldString +"\t"+ batch);// SQLHelp 是自己写的一个工具类SQLHelp sqlHelp =newSQLHelp();try{// 调用添加学生信息的方法
sqlHelp.addStudent(id, nameString, genderString, dobfieldString, batch);// 弹出对话框,提示用户添加成功JOptionPane.showMessageDialog(AddFrame.this,"添加成功!");}catch(SQLException e1){// 这里捕获一下异常,因为学生的学号是唯一的,所以当学号已经存在的时候,提示用户,该学生信息已存在// 异常代码为 23000if(e1.getSQLState().equals("23000")){JOptionPane.showMessageDialog(AddFrame.this,"添加失败!该学生已存在");}
e1.printStackTrace();}}});
addButton.setFont(newFont("宋体",Font.PLAIN,18));
addButton.setBounds(182,389,97,33);
panel.add(addButton);}}
我在这设置一个标题
作者写这边博客也不容易,对你有帮助就来个一键三连咋样?
对了有个不懂的地方可以留言或者私信,我看见了会回你的~
版权归原作者 猫腻余腥 所有, 如有侵权,请联系我们删除。