当前位置:   article > 正文

Java Swing+SQL sever学生管理系统代码_java+sqlserver学生成绩管理系统

java+sqlserver学生成绩管理系统

主界面(Interface类):

主界面:我设置了全局字体,字体的话,如果在每个每个类中设置的话,会显得很臃肿,所以我就写了一方法定义了一个全局字体,当然,在最后面有些特殊需要的字体,我就另外重新定义了字体,而这个全局字体的话,是对所有的字体进行规范的。

然后添加了背景图片:

设置背景图片的方法基本上都是这样,但是你要知道,Java的Swing窗口,你在添加这些组件的时候,如果你没有设置透明,它是会覆盖的,从而显示不出来,这也是我之前所遇到的难题,所以后面每个类,再添加这些东西的时候,我都会去设置三个盘子,去容纳所需要添加的组件,如果你是是使用C#的话,那就会方便很多!

  1. //设置背景图片
  2. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图一.jpg");
  3. JLabel bgimg = new JLabel(img);
  4. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());

主界面(Interface类)的源代码:

  1. package 高校学生管理库;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Enumeration;
  6. import javax.swing.*;
  7. import javax.swing.plaf.FontUIResource;
  8. public class Interface extends JFrame{
  9. private static final long serialVersionUID = 1L;
  10. final int WIDTH = 500;
  11. final int HEIGHT = 315;
  12. // 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体
  13. private static void InitGlobalFont(Font font) {
  14. FontUIResource fontRes = new FontUIResource(font);
  15. for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
  16. Object key = keys.nextElement();
  17. Object value = UIManager.get(key);
  18. if (value instanceof FontUIResource) {
  19. UIManager.put(key, fontRes);
  20. }
  21. }
  22. }
  23. public Interface() {
  24. setTitle("登录");
  25. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  27. this.setSize(520,370);
  28. this.setLocationRelativeTo(null);
  29. setVisible(true);
  30. //给盘子1创建对象
  31. javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
  32. jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
  33. jpanel_1.setLayout(null);//设置布局为空
  34. //给盘子2创建对象
  35. javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
  36. jpanel_2.setBounds(0,30,WIDTH,100);
  37. FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
  38. jpanel_2.setLayout(flowLayout);//设置布局为居中
  39. jpanel_2.setOpaque(false);//设置盘子为透明
  40. //给盘子3创建对象
  41. javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
  42. jpanel_3.setBounds(0,50,WIDTH,350);
  43. jpanel_3.setLayout(null);//设置布局为空
  44. jpanel_3.setOpaque(false);//设置盘子为透明
  45. //初始化字体
  46. font fonts = new font();
  47. //设置背景图片
  48. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图一.jpg");
  49. JLabel bgimg = new JLabel(img);
  50. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  51. JLabel title = new JLabel(" 我 是 ");
  52. title.setFont(font.title);
  53. title.setHorizontalAlignment(SwingConstants.CENTER);//居中
  54. JButton g=new JButton("管理员");g.setBounds(200, 80, 100, 40);
  55. JButton x=new JButton("学生");x.setBounds(200, 200, 100, 40);
  56. g.setContentAreaFilled(false);x.setContentAreaFilled(false);
  57. jpanel_3.add(g);jpanel_3.add(x);jpanel_2.add(title);
  58. jpanel_1.add(bgimg);this.add(jpanel_3);this.add(jpanel_2);
  59. this.add(jpanel_1);
  60. x.addActionListener(new ActionListener() {//监听学生按钮
  61. public void actionPerformed(ActionEvent e) {
  62. new Slogin();
  63. }
  64. });
  65. g.addActionListener(new ActionListener() {//监听管理员按钮
  66. public void actionPerformed(ActionEvent e) {
  67. new Glogin();
  68. }
  69. });
  70. }
  71. public static void main(String[] args) {
  72. new Interface().setVisible(true);
  73. InitGlobalFont(new Font("alias", Font.PLAIN, 18)); //统一设置字体
  74. }
  75. }

8e5a57c81f914215acce46723115edb6.png

管理员(Glogin)和学生(Slogin)登录界面:

这个登录界面的实现还是比较简单的,但是设置了验证码以及学生密码运用MD5加密!!这两个的运用应该是系统的灵魂所在了。

而什么是MD5加密呢?

MD5概述:
MD5消息摘要算法,属Hash算法一类。MD5算法对输入任意长度的消息进行运行,产生一个128位的消息摘要(32位的数字字母混合码)。

MD5主要特点:
不可逆,相同数据的MD5值肯定一样,不同数据的MD5值不一样

(一个MD5理论上的确是可能对应无数多个原文的,因为MD5是有限多个的而原文可以是无数多个。比如主流使用的MD5将任意长度的“字节串映射为一个128bit的大整数。也就是一共有2^128种可能,大概是3.4*10^38,这个数字是有限多个的,而但是世界上可以被用来加密的原文则会有无数的可能性)

MD5的性质:
1、压缩性:任意长度的数据,算出的MD5值长度都是固定的(相当于超损压缩)。

2、容易计算:从原数据计算出MD5值很容易。

3、抗修改性:对原数据进行任何改动,哪怕只修改1个字节,所得到的MD5值都有很大区别。

4、弱抗碰撞:已知原数据和其MD5值,想找到一个具有相同MD5值的数据(即伪造数据)是非常困难的。

5、强抗碰撞:想找到两个不同的数据,使它们具有相同的MD5值,是非常困难的。

为了大家更好地理解,我推荐一篇文章:java中MD5加密 - zpk-aaron - 博客园 (cnblogs.com)

管理员(Glogin)界面源代码:

  1. package 高校学生管理库;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Statement;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import javax.swing.*;
  11. public class Glogin extends JFrame{
  12. private static final long serialVersionUID = 1L;
  13. private Captcha vcode = new Captcha();
  14. JTextField co;
  15. //窗口常量
  16. final int WIDTH = 500;
  17. final int HEIGHT = 315;
  18. public Glogin() {
  19. super("管理员登陆");
  20. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  21. this.setSize(520,370);
  22. this.setLocationRelativeTo(null);
  23. setVisible(true);
  24. //初始化字体
  25. font fonts = new font();
  26. //给盘子1创建对象
  27. javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
  28. jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
  29. jpanel_1.setLayout(null);//设置布局为空
  30. //给盘子2创建对象
  31. javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
  32. jpanel_2.setBounds(0,30,WIDTH,100);
  33. FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
  34. jpanel_2.setLayout(flowLayout);//设置布局为居中
  35. jpanel_2.setOpaque(false);//设置盘子为透明
  36. //给盘子3创建对象
  37. javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
  38. jpanel_3.setBounds(0,50,WIDTH,350);
  39. jpanel_3.setLayout(null);//设置布局为空
  40. jpanel_3.setOpaque(false);//设置盘子为透明
  41. //设置背景图片
  42. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图三.jpg");
  43. JLabel bgimg = new JLabel(img);
  44. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  45. //登录界面
  46. JLabel title = new JLabel("管理员登录界面");
  47. title.setFont(font.title);
  48. JLabel l= new JLabel("账号");l.setBounds(80, 50, 60, 40);
  49. JLabel p= new JLabel("密码");p.setBounds(80, 100, 60, 40);
  50. JLabel code=new JLabel("验证码");code.setBounds(70, 150, 60, 40);
  51. JTextField user=new JTextField();user.setBounds(150, 50, 150, 30);
  52. JPasswordField pass=new JPasswordField();pass.setBounds(150, 100, 150, 30);
  53. co=new JTextField();co.setBounds(150, 150, 150, 30);
  54. JButton ok=new JButton("登录");ok.setBounds(180, 220, 70, 30);
  55. ok.setContentAreaFilled(false);vcode.setBounds(310, 145, 100, 40);
  56. jpanel_3.add(l);jpanel_3.add(p);jpanel_3.add(code);
  57. jpanel_3.add(user);jpanel_3.add(co);jpanel_3.add(vcode);
  58. jpanel_3.add(pass);jpanel_3.add(ok);jpanel_2.add(title);
  59. jpanel_1.add(bgimg);
  60. this.add(jpanel_3);this.add(jpanel_2);this.add(jpanel_1);
  61. ok.addActionListener(new ActionListener() {//监听登录按钮
  62. public void actionPerformed(ActionEvent e) {
  63. String jusername=user.getText();
  64. char s[]=pass.getPassword();
  65. String jpassword=new String(s);
  66. String coo=co.getText();
  67. try {
  68. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  69. //加载对应的jdbc驱动
  70. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  71. //配置连接字符串
  72. String user="sa";//sa超级管理员
  73. String password="你的密码";//密码
  74. Connection conn=DriverManager.getConnection(url,user,password);
  75. //创建数据库连接对象
  76. Statement st=conn.createStatement();
  77. //创建SQL语句执行对象
  78. String strSQL="(Select * from dbo.GLY where ID1='"+jusername+"'And PAWD1='"+jpassword+"' )";
  79. ResultSet rs=st.executeQuery(strSQL);
  80. if(coo.isEmpty()) {
  81. JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
  82. }
  83. else {
  84. if(!isValidCodeRight()) {
  85. JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
  86. }
  87. else {
  88. if(rs.next()) {
  89. new GLY();
  90. closeThis();
  91. }
  92. else {
  93. JOptionPane.showMessageDialog(null,"用户名不存在或密码错误","错误!",JOptionPane.ERROR_MESSAGE);
  94. }
  95. conn.close();//关闭数据库连接
  96. }
  97. }
  98. }
  99. catch (ClassNotFoundException ex) {
  100. System.out.println("没有找到对应的数据库驱动类");
  101. }
  102. catch (SQLException ex) {
  103. System.out.println("数据库连接或者是数据库操作失败");
  104. }
  105. }
  106. });
  107. }
  108. public boolean isValidCodeRight() {
  109. if(co == null) {
  110. return false;
  111. }else if(vcode == null) {
  112. return true;
  113. }else if(vcode.getCode() .equals(co.getText())) {
  114. return true;
  115. }else
  116. return false;
  117. }
  118. public void closeThis() {//关闭当前界面
  119. this.dispose();
  120. }
  121. }

0555bc96ca1e4d63a57e1816cf423e1c.png

学生(Slogin)界面源代码:

  1. package 高校学生管理库;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.UnsupportedEncodingException;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.sql.Statement;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import javax.swing.*;
  13. public class Slogin extends JFrame {
  14. private static final long serialVersionUID = 1L;
  15. private Captcha vcode = new Captcha();
  16. JTextField co;
  17. static JTextField user;
  18. //窗口常量
  19. final int WIDTH = 500;
  20. final int HEIGHT = 315;
  21. public Slogin() {
  22. super("学生登陆");
  23. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  24. this.setSize(520,370);
  25. this.setLocationRelativeTo(null);
  26. setVisible(true);
  27. //初始化字体
  28. font fonts = new font();
  29. //给盘子1创建对象
  30. javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
  31. jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
  32. jpanel_1.setLayout(null);//设置布局为空
  33. //给盘子2创建对象
  34. javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
  35. jpanel_2.setBounds(0,30,WIDTH,100);
  36. FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);//设置布局为居中
  37. jpanel_2.setLayout(flowLayout);
  38. jpanel_2.setOpaque(false);//设置盘子为透明
  39. //给盘子3创建对象
  40. javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
  41. jpanel_3.setBounds(0,50,WIDTH,350);
  42. jpanel_3.setLayout(null);//设置布局为空
  43. jpanel_3.setOpaque(false);//设置盘子为透明
  44. //设置背景图片
  45. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图二.jpg");
  46. JLabel bgimg = new JLabel(img);
  47. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  48. //登录界面
  49. JLabel title = new JLabel("学生登录界面");
  50. title.setFont(font.title);
  51. JLabel l= new JLabel("账号");l.setBounds(80, 50, 60, 40);
  52. JLabel p= new JLabel("密码");p.setBounds(80, 100, 60, 40);
  53. JLabel code=new JLabel("验证码");code.setBounds(70, 150, 60, 40);
  54. user=new JTextField();user.setBounds(150, 50, 150, 30);
  55. JPasswordField pass=new JPasswordField();pass.setBounds(150, 100, 150, 30);
  56. co=new JTextField();co.setBounds(150, 150, 150, 30);
  57. JButton ok=new JButton("登录"); ok.setBounds(120, 220, 70, 30);
  58. ok.setContentAreaFilled(false);
  59. JButton register=new JButton("注册");register.setBounds(250, 220, 70, 30);
  60. register.setContentAreaFilled(false);
  61. vcode.setBounds(310, 145, 100, 40);
  62. jpanel_3.add(l);jpanel_3.add(p);jpanel_3.add(code);
  63. jpanel_3.add(user);jpanel_3.add(co);jpanel_3.add(vcode);
  64. jpanel_3.add(register);jpanel_3.add(pass);jpanel_3.add(ok);
  65. jpanel_2.add(title);jpanel_1.add(bgimg);
  66. this.add(jpanel_3);this.add(jpanel_2);this.add(jpanel_1);
  67. register.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69. new Enroll();
  70. closeThis();
  71. }
  72. });
  73. ok.addActionListener(new ActionListener() { //监听登录按钮
  74. public void actionPerformed(ActionEvent e) {
  75. String jusername=user.getText();
  76. char s[]=pass.getPassword();
  77. String jpassword=new String(s);
  78. String coo=co.getText();
  79. try {
  80. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  81. //加载对应的jdbc驱动
  82. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  83. //配置连接字符串
  84. String user="sa";//sa超级管理员
  85. String password="你的密码";//密码
  86. Connection conn=DriverManager.getConnection(url,user,password);
  87. //创建数据库连接对象
  88. Statement st=conn.createStatement();
  89. //创建SQL语句执行对象
  90. Md5 md5 = new Md5();//使用MD5加密
  91. String newString = md5.EncoderByMd5(jpassword);
  92. String strSQL="(Select * from dbo.PY where ID='"+jusername+"'And PAWD='"+newString+"' )";
  93. ResultSet rs=st.executeQuery(strSQL);
  94. if(coo.isEmpty()) {
  95. JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
  96. }
  97. else {
  98. if(!isValidCodeRight()) {
  99. JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
  100. }
  101. else {
  102. if(rs.next()) {
  103. new Student();
  104. closeThis();
  105. }
  106. else {
  107. JOptionPane.showMessageDialog(null,"用户名不存在或密码错误","错误!",JOptionPane.ERROR_MESSAGE);
  108. }
  109. conn.close();//关闭数据库连接
  110. }
  111. }
  112. }
  113. catch (ClassNotFoundException ex) {
  114. System.out.println("没有找到对应的数据库驱动类");
  115. }
  116. catch (SQLException ex) {
  117. System.out.println("数据库连接或者是数据库操作失败");
  118. }catch (NoSuchAlgorithmException e1) {
  119. e1.printStackTrace();
  120. } catch (UnsupportedEncodingException e1) {
  121. e1.printStackTrace();
  122. }
  123. }
  124. });
  125. }
  126. public boolean isValidCodeRight() {
  127. if(co == null) {
  128. return false;
  129. }else if(vcode == null) {
  130. return true;
  131. }else if(vcode.getCode() .equals(co.getText())) {
  132. return true;
  133. }else
  134. return false;
  135. }
  136. public void closeThis() {//关闭当前界面
  137. this.dispose();
  138. }
  139. }

e47d12e7c2c248f49b34fb49a8744a6a.png

 MD5类源代码:

  1. package 高校学生管理库;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import sun.misc.BASE64Encoder;
  6. public class Md5 { /**利用MD5进行加密*/
  7. public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
  8. //确定计算方法
  9. MessageDigest md5=MessageDigest.getInstance("MD5");
  10. BASE64Encoder base64en = new BASE64Encoder();
  11. //加密后的字符串
  12. String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
  13. return newstr;
  14. }
  15. }

验证码功能(Captcha类)源代码:区分大小写,点击可更换

  1. package 高校学生管理库;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import java.util.Random;
  11. import javax.swing.JComponent;
  12. public class Captcha extends JComponent implements MouseListener {
  13. private static final long serialVersionUID = 1L;
  14. private String code;
  15. private int width, height = 40;
  16. private int codeLength = 4;
  17. private Random random = new Random();
  18. public Captcha() {
  19. width = this.codeLength * 16 + (this.codeLength - 1) * 10;
  20. setPreferredSize(new Dimension(width, height));
  21. setSize(width, height);
  22. this.addMouseListener(this);
  23. setToolTipText("点击可以更换验证码");
  24. }
  25. public int getCodeLength() {
  26. return codeLength;
  27. }
  28. //设置验证码文字的长度
  29. public void setCodeLength(int codeLength) {
  30. if(codeLength < 4) {
  31. this.codeLength = 4;
  32. } else {
  33. this.codeLength = codeLength;
  34. }
  35. }
  36. public String getCode() {
  37. return code;
  38. }
  39. //产生随机的颜色
  40. public Color getRandColor(int min, int max) {
  41. if (min > 255)
  42. min = 255;
  43. if (max > 255)
  44. max = 255;
  45. int red = random.nextInt(max - min) + min;
  46. int green = random.nextInt(max - min) + min;
  47. int blue = random.nextInt(max - min) + min;
  48. return new Color(red, green, blue);
  49. }
  50. //设置验证码具体的字母是什么
  51. protected String generateCode() {
  52. char[] codes = new char[this.codeLength];
  53. for (int i = 0, len = codes.length; i < len; i++) {
  54. if (random.nextBoolean()) {
  55. codes[i] = (char) (random.nextInt(26) + 65);
  56. } else {
  57. codes[i] = (char) (random.nextInt(26) + 97);
  58. }
  59. }
  60. this.code = new String(codes);
  61. return this.code;
  62. }
  63. @Override
  64. protected void paintComponent(Graphics g) {
  65. super.paintComponent(g);
  66. if(this.code == null || this.code.length() != this.codeLength) {
  67. this.code = generateCode();
  68. }
  69. width = this.codeLength * 16 + (this.codeLength - 1) * 10;
  70. super.setSize(width, height);
  71. super.setPreferredSize(new Dimension(width, height));
  72. Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
  73. g.setFont(mFont);
  74. //绘制出验证码的背景的矩形轮廓
  75. Graphics2D g2d = (Graphics2D) g;
  76. g2d.setColor(getRandColor(200, 250));
  77. g2d.fillRect(0, 0, width, height);
  78. g2d.setColor(getRandColor(180, 200));
  79. g2d.drawRect(0, 0, width - 1, height - 1);
  80. //绘制出验证码背景的线
  81. int i = 0, len = 150;
  82. for (; i < len; i++) {
  83. int x = random.nextInt(width - 1);
  84. int y = random.nextInt(height - 1);
  85. int x1 = random.nextInt(width - 10) + 10;
  86. int y1 = random.nextInt(height - 4) + 4;
  87. g2d.setColor(getRandColor(180, 200));
  88. g2d.drawLine(x, y, x1, y1);
  89. }
  90. //绘制出验证码的具体字母
  91. i = 0; len = this.codeLength;
  92. FontMetrics fm = g2d.getFontMetrics();
  93. int base = (height - fm.getHeight())/2 + fm.getAscent();
  94. for(;i<len;i++) {
  95. int b = random.nextBoolean() ? 1 : -1;
  96. g2d.rotate(random.nextInt(10)*0.01*b);
  97. g2d.setColor(getRandColor(20, 130));
  98. g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);
  99. }
  100. }
  101. //下一个验证码
  102. public void nextCode() {
  103. generateCode();
  104. repaint();
  105. }
  106. @Override
  107. public void mouseClicked(MouseEvent e) {
  108. nextCode();
  109. }
  110. @Override
  111. public void mousePressed(MouseEvent e) {
  112. }
  113. @Override
  114. public void mouseReleased(MouseEvent e) {
  115. }
  116. @Override
  117. public void mouseEntered(MouseEvent e) {
  118. }
  119. @Override
  120. public void mouseExited(MouseEvent e) {
  121. }
  122. }

学生注册(Enroll类):

学生注册界面:
上传照片功能,这里我遇到了一个问题就是关于转义字符的问题,因为我要获取选中的图片的路径并将照片添加到JLabel里展示出来,当我们需要在字符串中使用普通的反斜杠时,就需要使用双反斜杠\\来表示;java编译器会将\\\\ 解释为 \\ ,而正则表达式里,双斜杠 \\ 代表单斜杠 \ ,这也就是为什么要用 \\\\ 表示 \ ,\\\\\\\\表示\\。

  1. pho.addActionListener(new ActionListener() {
  2. public void actionPerformed(ActionEvent e) {
  3. JFileChooser fileChooser = new JFileChooser("C:\\Users\\god36c\\OneDrive\\桌面");
  4. fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  5. int returnVal = fileChooser.showOpenDialog(fileChooser);
  6. if(returnVal == JFileChooser.APPROVE_OPTION) {
  7. File filePath = fileChooser.getSelectedFile();//获取图片路径
  8. System.out.println(filePath);
  9. String f=filePath.getPath();
  10. String filePath1=f.replaceAll("\\\\\\\\", "\\\\\\\\"); //将\转义为\\
  11. ImageIcon p = new ImageIcon(filePath1);
  12. photo.setIcon(p);
  13. }
  14. }
  15. });

然后这里用到了数据库中的表来判断是否已经存在这个人,如果存在注册失败,不存在并信息填入符合要求则成功(照片则保存照片的路径)。

学生注册界面源代码:

  1. package 高校学生管理库;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.File;
  6. import java.io.UnsupportedEncodingException;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import javax.swing.*;
  15. public class Enroll extends JFrame{
  16. private static final long serialVersionUID = 1L;
  17. JTextField co;
  18. private Captcha vcode = new Captcha();
  19. final int WIDTH = 1020;
  20. final int HEIGHT = 1615;
  21. public Enroll() {
  22. super("注册");
  23. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. this.setSize(1020,700);
  25. this.setLocationRelativeTo(null); //此窗口将置于屏幕的中央
  26. setVisible(true);
  27. //初始化字体
  28. font fonts = new font();
  29. //设置背景图片
  30. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图七.jpg");
  31. JLabel bgimg = new JLabel(img);
  32. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  33. bgimg.setOpaque(false);//背景照片透明
  34. //给盘子1创建对象
  35. javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
  36. jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
  37. jpanel_1.setLayout(null);//设置布局为空
  38. jpanel_1.setOpaque(false);
  39. //给盘子2创建对象
  40. javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
  41. jpanel_2.setBounds(0,30,WIDTH,100);
  42. FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
  43. jpanel_2.setLayout(flowLayout);//设置布局为居中
  44. jpanel_2.setOpaque(false);//设置盘子为透明
  45. //给盘子3创建对象
  46. javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
  47. jpanel_3.setBounds(0,50,WIDTH,350);
  48. jpanel_3.setLayout(null);//设置布局为空
  49. jpanel_3.setOpaque(false);//设置盘子为透明
  50. JLabel title = new JLabel(" 注 册 界 面 ");
  51. title.setFont(font.title);
  52. title.setHorizontalAlignment(SwingConstants.CENTER);//居中
  53. JLabel username=new JLabel("账号");username.setBounds(58, 46, 60, 40);
  54. JLabel password=new JLabel("密码");password.setBounds(54,100,120,30);
  55. JLabel id=new JLabel("学号");id.setBounds(54,150,120,30);
  56. JLabel phone=new JLabel("手机号码");phone.setBounds(22,200,120,30);
  57. JLabel code=new JLabel("验证码");code.setBounds(41,250,120,30);
  58. JLabel photo=new JLabel();photo.setBounds(280,100,200,200);
  59. JTextField user=new JTextField();user.setBounds(100,50,120,30);
  60. JTextField pass=new JTextField(); pass.setBounds(100,100,120,30);
  61. JTextField idd=new JTextField();idd.setBounds(100,150,120,30);
  62. JTextField ph=new JTextField();ph.setBounds(100,200,120,30);
  63. co=new JTextField();co.setBounds(100,250,120,30);
  64. JButton pho=new JButton("上传照片");pho.setBounds(305,280,100,30);
  65. JButton register=new JButton("注 册"); register.setBounds(150,380,80,30);
  66. JButton exit=new JButton("退 出");exit.setBounds(250,380,80,30);
  67. pho.setContentAreaFilled(false);register.setContentAreaFilled(false);exit.setContentAreaFilled(false);
  68. vcode.setBounds(112, 300, 100, 40);
  69. jpanel_1.add(username);jpanel_1.add(user);jpanel_1.add(password);
  70. jpanel_1.add(pass);jpanel_1.add(id);jpanel_1.add(idd);
  71. jpanel_1.add(phone);jpanel_1.add(ph);jpanel_1.add(code);
  72. jpanel_1.add(co);jpanel_1.add(photo);jpanel_1.add(pho);
  73. jpanel_1.add(register);jpanel_1.add(exit);jpanel_1.add(vcode);
  74. jpanel_1.add(bgimg);this.add(jpanel_1);jpanel_2.add(title);
  75. this.add(jpanel_2);this.add(jpanel_3);
  76. pho.addActionListener(new ActionListener() {
  77. public void actionPerformed(ActionEvent e) {
  78. JFileChooser fileChooser = new JFileChooser("C:\\Users\\god36c\\OneDrive\\桌面");
  79. fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  80. int returnVal = fileChooser.showOpenDialog(fileChooser);
  81. if(returnVal == JFileChooser.APPROVE_OPTION) {
  82. File filePath = fileChooser.getSelectedFile();//获取图片路径
  83. System.out.println(filePath);
  84. String f=filePath.getPath();
  85. String filePath1=f.replaceAll("\\\\\\\\", "\\\\\\\\"); //将\转义为\\
  86. ImageIcon p = new ImageIcon(filePath1);
  87. photo.setIcon(p);
  88. }
  89. }
  90. });
  91. register.addActionListener(new ActionListener() {
  92. public void actionPerformed(ActionEvent e) {
  93. String user1=user.getText().trim();
  94. String pass1=pass.getText().trim();
  95. String id1=idd.getText().trim();
  96. String ph1=ph.getText().trim();
  97. String co1=co.getText();
  98. try {
  99. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  100. //加载对应的jdbc驱动
  101. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  102. //配置连接字符串
  103. String user="sa";//sa超级管理员
  104. String password="你的密码";//密码
  105. Connection conn=DriverManager.getConnection(url,user,password);
  106. //创建数据库连接对象
  107. Statement st=conn.createStatement();
  108. //创建SQL语句执行对象
  109. String strSQL="(Select * from dbo.PY where ID='"+user1+"' )";
  110. ResultSet rs=st.executeQuery(strSQL);
  111. Md5 md5 = new Md5();
  112. String newString = md5.EncoderByMd5(pass1);
  113. if(co1.isEmpty()) {
  114. JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
  115. }
  116. else {
  117. if(!isValidCodeRight()) {
  118. JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
  119. }
  120. else {
  121. if(rs.next()) {
  122. JOptionPane.showMessageDialog(null,"用户名已存在","错误!", JOptionPane.ERROR_MESSAGE);
  123. }
  124. else {
  125. String sql = "insert into dbo.PY(ID,PAWD,Sno,phone) values('"+user1+"','"+newString+"','"+id1+"','"+ph1+"')";
  126. PreparedStatement pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
  127. pst.executeUpdate();
  128. pst.close();
  129. JOptionPane.showMessageDialog(null,"注册成功");
  130. }
  131. conn.close();//关闭数据库连接
  132. }
  133. }
  134. }
  135. catch (ClassNotFoundException ex) {
  136. System.out.println("没有找到对应的数据库驱动类");
  137. }
  138. catch (SQLException ex) {
  139. System.out.println("数据库连接或者是数据库操作失败");
  140. }
  141. catch (NoSuchAlgorithmException e1) {
  142. e1.printStackTrace();
  143. } catch (UnsupportedEncodingException e1) {
  144. e1.printStackTrace();
  145. }
  146. }
  147. });
  148. exit.addActionListener(new ActionListener() {
  149. public void actionPerformed(ActionEvent e) {
  150. closeThis();
  151. new Slogin();
  152. }
  153. });
  154. }
  155. public boolean isValidCodeRight() {
  156. if(co == null) {
  157. return false;
  158. }else if(vcode == null) {
  159. return true;
  160. }else if(vcode.getCode() .equals(co.getText())) {
  161. return true;
  162. }else
  163. return false;
  164. }
  165. public void closeThis(){//关闭当前界面
  166. this.dispose();
  167. }
  168. }

学生类(Student):

另外,大家要注意数据库链接的时候,我的数据库是和你们不一样的,所以你们要留意一下,自己的数据库是否和我一样,如果不一样要根据自己的数据库去更改一下,不然是链接不成功的!!接下来的学生和管理员类,是这次系统设计的核心。

Student的源代码:

  1. package 高校学生管理库;
  2. import java.awt.Dimension;
  3. import java.awt.Menu;
  4. import java.awt.MenuBar;
  5. import java.awt.MenuItem;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.sql.Connection;
  9. import java.sql.Statement;
  10. import java.util.Vector;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPanel;
  20. import javax.swing.JScrollPane;
  21. import javax.swing.JTabbedPane;
  22. import javax.swing.JTable;
  23. import javax.swing.JTextField;
  24. import javax.swing.table.DefaultTableModel;
  25. public class Student extends JFrame{
  26. private static final long serialVersionUID = 1L;
  27. public Student() throws SQLException, ClassNotFoundException
  28. {
  29. super("学生登陆");
  30. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31. this.setSize(800,600);
  32. this.setLocationRelativeTo(null);
  33. setVisible(true);
  34. MenuBar bar = new MenuBar();// 创建菜单栏
  35. Menu fileMenu = new Menu("FILE");// 创建“文件”菜单
  36. MenuItem open = new MenuItem("OPEN");
  37. MenuItem exit = new MenuItem("EXIT");
  38. Menu help = new Menu("HELP");// 创建“帮助"菜单
  39. MenuItem print = new MenuItem("PRINT");
  40. exit.addActionListener(new ActionListener(){
  41. public void actionPerformed(ActionEvent e)
  42. {
  43. new Slogin();
  44. closeThis();
  45. }
  46. });
  47. //设置背景图片
  48. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图八.jpg");
  49. JLabel bgimg = new JLabel(img);
  50. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  51. bgimg.setOpaque(false);//背景照片透明
  52. //设置背景图片
  53. ImageIcon img1 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十.jpg");
  54. JLabel bgimg1 = new JLabel(img1);
  55. bgimg1.setBounds(0,0,img1.getIconWidth(),img1.getIconHeight());
  56. bgimg1.setOpaque(false);//背景照片透明
  57. //设置背景图片
  58. ImageIcon img2 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图九.jpg");
  59. JLabel bgimg2 = new JLabel(img2);
  60. bgimg2.setBounds(0,0,img2.getIconWidth(),img2.getIconHeight());
  61. bgimg2.setOpaque(false);//背景照片透明
  62. fileMenu.add(print);fileMenu.add(open);
  63. fileMenu.addSeparator();// 设置菜单分隔符
  64. fileMenu.add(exit);
  65. bar.add(fileMenu);// 将文件添加到菜单栏上
  66. bar.add(help);// 将文件添加到菜单栏上
  67. setMenuBar(bar);// 设置菜单栏,使用这种方式设置菜单栏可以不占用布局空间
  68. //创建组件
  69. JPanel jp1= new JPanel();//定义面板
  70. JPanel jp2= new JPanel();
  71. JPanel jp3= new JPanel();
  72. jp1.setLayout(null);//自由布局
  73. jp2.setLayout(null);//自由布局
  74. jp3.setLayout(null);//自由布局
  75. //jp1面板上上的内容
  76. String[][] datas = {};
  77. String[] titles = { "学号", "姓名","性别","年龄","专业" };
  78. String[][] datas1 = {};
  79. String[] titles1 = { "课程号", "分数","等级"};
  80. DefaultTableModel myModel = new DefaultTableModel(datas, titles);// myModel存放表格的数据
  81. DefaultTableModel myModel1 = new DefaultTableModel(datas1, titles1);
  82. JTable table = new JTable(myModel);// 表格对象table的数据来源是myModel对象
  83. JTable table1 = new JTable(myModel1);
  84. table.setPreferredScrollableViewportSize(new Dimension(550, 100));// 表格的显示尺寸
  85. table1.setPreferredScrollableViewportSize(new Dimension(550, 100));
  86. // 产生一个带滚动条的面板
  87. JScrollPane scrollPane = new JScrollPane(table);scrollPane.setBounds(50, 190, 550, 70);
  88. JScrollPane scrollPane1 = new JScrollPane(table1);scrollPane1.setBounds(50, 290, 550, 100);
  89. //行高
  90. table.setRowHeight(20);
  91. table1.setRowHeight(20);
  92. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  93. //加载对应的jdbc驱动
  94. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  95. //配置连接字符串
  96. String user="sa";//sa超级管理员
  97. String password="123456";//密码
  98. Connection conn=DriverManager.getConnection(url,user,password);
  99. //创建数据库连接对象
  100. Statement st=conn.createStatement();
  101. //创建SQL语句执行对象
  102. String strSQL="(Select * from dbo.Student where Sname='"+ Slogin.user.getText()+"')";
  103. ResultSet rs=st.executeQuery(strSQL);
  104. if(rs.next()) {
  105. Vector<String> ve = new Vector<String>();
  106. ve.addElement(rs.getString(1));ve.addElement(rs.getString(2));
  107. ve.addElement(rs.getString(3));ve.addElement(rs.getString(4));
  108. ve.addElement(rs.getString(5));
  109. myModel.addRow(ve);
  110. }
  111. String s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Student.Sno=SC.Sno)";
  112. ResultSet r1=st.executeQuery(s1);
  113. while(r1.next()) {
  114. Vector<String> ve1 = new Vector<String>();
  115. ve1.addElement(r1.getString(7));
  116. ve1.addElement(r1.getString(8));
  117. ve1.addElement(r1.getString(9));
  118. myModel1.addRow(ve1);
  119. }
  120. JButton again=new JButton("刷 新~");again.setBounds(490, 140, 80, 30);
  121. again.setContentAreaFilled(false);
  122. again.addActionListener(new ActionListener(){
  123. public void actionPerformed(ActionEvent e) {
  124. try {
  125. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  126. //加载对应的jdbc驱动
  127. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  128. //配置连接字符串
  129. String user="sa";//sa超级管理员
  130. String password="你的密码";//密码
  131. Connection conn=DriverManager.getConnection(url,user,password);
  132. Statement st=conn.createStatement();
  133. while(myModel1.getRowCount()>0) {
  134. myModel1.removeRow(myModel1.getRowCount()-1);
  135. }
  136. String s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Student.Sno=SC.Sno)";
  137. ResultSet r1=st.executeQuery(s1);
  138. while(r1.next()) {
  139. Vector<String> ve1 = new Vector<String>();
  140. ve1.addElement(r1.getString(7));
  141. ve1.addElement(r1.getString(8));
  142. ve1.addElement(r1.getString(9));
  143. myModel1.addRow(ve1);
  144. }
  145. conn.close();
  146. }catch (ClassNotFoundException ex) {
  147. System.out.println("没有找到对应的数据库驱动类");
  148. }
  149. catch (SQLException ex) {
  150. System.out.println("数据库连接或者是数据库操作失败");
  151. }
  152. }
  153. });
  154. //jp2面板上的内容
  155. String[][] datas2 = {};
  156. String[] titles2 = { "课程号", "课程名","学分" };
  157. DefaultTableModel myModel2 = new DefaultTableModel(datas2, titles2);
  158. JTable table2 = new JTable(myModel2);
  159. table2.setRowHeight(20);
  160. table2.setPreferredScrollableViewportSize(new Dimension(550, 400));
  161. JScrollPane scrollPane2 = new JScrollPane(table2);scrollPane2.setBounds(50, 20, 550, 400);
  162. String s2="(Select * from dbo.Course)";
  163. ResultSet r2=st.executeQuery(s2);
  164. while(r2.next()) {
  165. Vector<String> ve2 = new Vector<String>();
  166. ve2.addElement(r2.getString(1));
  167. ve2.addElement(r2.getString(2));
  168. ve2.addElement(r2.getString(4));
  169. myModel2.addRow(ve2);
  170. }
  171. conn.close();
  172. //jp2上的内容
  173. JLabel a=new JLabel("请输入你想选的课的课程号:");a.setBounds(50, 470, 270, 30);
  174. JTextField b=new JTextField(20);b.setBounds(320, 470, 150, 25);
  175. JButton c=new JButton("确定");c.setBounds(500, 470, 80, 27);
  176. //jp3上的内容
  177. JLabel ja1=new JLabel("你想查询的科目是:");ja1.setBounds(50, 50, 200, 30);
  178. JLabel ja2=new JLabel("你的成绩是:");ja2.setBounds(80, 220, 150, 30);
  179. JLabel ja3=new JLabel("你的等级是:");ja3.setBounds(80, 270, 150, 30);
  180. JLabel ja4=new JLabel("(输入课程号哦~)");ja4.setBounds(255, 80, 150, 30);
  181. JTextField b1=new JTextField(15);b1.setBounds(260, 50, 150, 25);
  182. JTextField b2=new JTextField(15);b2.setBounds(260, 220, 100, 25);
  183. JTextField b3=new JTextField(15);b3.setBounds(260, 270, 100, 25);
  184. JButton c1=new JButton("查 询");c1.setBounds(450, 50, 70, 30);
  185. // 将组件添加入jp1窗口中
  186. jp1.add(scrollPane);jp1.add(scrollPane1);
  187. jp1.add(again);jp1.add(bgimg);
  188. // 将组件添加入jp2窗口中
  189. jp2.add(scrollPane2);jp2.add(a);jp2.add(b);
  190. jp2.add(c);jp2.add(bgimg1);
  191. // 将组件添加入jp3窗口中
  192. jp3.add(ja1);jp3.add(ja2);jp3.add(ja3);
  193. jp3.add(ja4);jp3.add(b1);jp3.add(b2);
  194. jp3.add(b3);jp3.add(c1);jp3.add(bgimg2);
  195. JTabbedPane jtbp=new JTabbedPane(JTabbedPane.LEFT); //创建选项卡并使选项卡垂直排列
  196. jtbp.add("个人信息",jp1);jtbp.add("选课",jp2);jtbp.add("成绩查询",jp3);
  197. this.add(jtbp); //添加选项卡窗格到容器
  198. c.addActionListener(new ActionListener(){
  199. public void actionPerformed(ActionEvent e)
  200. {
  201. try {
  202. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  203. //加载对应的jdbc驱动
  204. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  205. //配置连接字符串
  206. String user="sa";//sa超级管理员
  207. String password="你的密码";//密码
  208. Connection conn=DriverManager.getConnection(url,user,password);
  209. Statement st=conn.createStatement();
  210. String ok=b.getText().trim();
  211. String s="(Select * from dbo.Course where Cno='"+ok+"' )";
  212. ResultSet r=st.executeQuery(s);
  213. if(r.next()) {
  214. String s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And SC.Sno=Student.Sno )";
  215. ResultSet r1=st.executeQuery(s1);
  216. if(r1.next()) {
  217. String s2="(Select * from dbo.SC where Sno='"+r1.getString(1)+"' And Cno='"+ok+"' )";
  218. ResultSet r2=st.executeQuery(s2);
  219. if(r2.next()) {
  220. JOptionPane.showMessageDialog(null, "你已经选过该科目了~","提示消息",JOptionPane.WARNING_MESSAGE);
  221. }
  222. else {
  223. String ss="(Select * from dbo.Student where Sname='"+ Slogin.user.getText()+"')";
  224. ResultSet rr=st.executeQuery(ss);
  225. if(rr.next()) {
  226. String strSQL="insert into dbo.SC(Sno,Cno) values('"+rr.getString(1)+"','"+ok+"')";
  227. int rr1=st.executeUpdate(strSQL);
  228. if(rr1==1) {
  229. JOptionPane.showMessageDialog(null, "选课成功","提示消息",JOptionPane.WARNING_MESSAGE);
  230. }
  231. }
  232. }
  233. }
  234. }
  235. else {
  236. JOptionPane.showMessageDialog(null, "没有这种科目哦~","提示消息",JOptionPane.WARNING_MESSAGE);
  237. }
  238. conn.close();
  239. }catch (ClassNotFoundException ex) {
  240. System.out.println("没有找到对应的数据库驱动类");
  241. }
  242. catch (SQLException ex) {
  243. System.out.println("数据库连接或者是数据库操作失败");
  244. }
  245. }
  246. });
  247. c1.addActionListener(new ActionListener(){
  248. public void actionPerformed(ActionEvent e) {
  249. try {
  250. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  251. //加载对应的jdbc驱动
  252. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  253. //配置连接字符串
  254. String user="sa";//sa超级管理员
  255. String password="你的密码";//密码
  256. Connection conn=DriverManager.getConnection(url,user,password);
  257. Statement st=conn.createStatement();
  258. String B1=b1.getText().trim();
  259. String L="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Cno='"+B1+"' And Student.Sno=SC.Sno )";
  260. ResultSet M=st.executeQuery(L);
  261. if(M.next()) {
  262. b2.setText(M.getString(8));
  263. b3.setText(M.getString(9));
  264. }
  265. else {
  266. JOptionPane.showMessageDialog(null, "没有该科目的成绩哦~","提示消息",JOptionPane.WARNING_MESSAGE);
  267. }
  268. conn.close();
  269. }catch (ClassNotFoundException ex) {
  270. System.out.println("没有找到对应的数据库驱动类");
  271. }
  272. catch (SQLException ex) {
  273. System.out.println("数据库连接或者是数据库操作失败");
  274. }
  275. }
  276. });
  277. }
  278. public void closeThis() {//关闭当前界面
  279. this.dispose();
  280. }
  281. }

a9cb386529474543a814288b946150e5.png

73e265aad61441e197a3d33d09ebebda.png 

 614ef65dc4fe46f7a85bac1d1f6d606d.png

 管理员(GLY)类:

这个管理员类,主要是显示和查询功能,当然,管理员这上面还是有一些组件,增删改的跳转,以及我们可以全部查询,也可以按学生学号进行查询,以及显示各种,专业,但是要记住的是,如果你没有注册学生账号,那么你按学号查询的话是只能查询到,所选的课程和课程号等,是不能查询到学生的其他信息的,简单来说就是,你只能查询到,最下面两组的信息,但是,如果你是按我的代码去写的话,那就是不会有任何问题,但是你如果是自己去添加什么功能的话,那么你的数据库列哪里就会发生改变,你就自己去调整和更改那些列,不然,是显示不出来的!!

GLY源代码;

  1. package 高校学生管理库;
  2. import java.awt.Dimension;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.Vector;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JSplitPane;
  18. import javax.swing.JTable;
  19. import javax.swing.JTextField;
  20. import javax.swing.table.DefaultTableModel;
  21. public class GLY extends JFrame {
  22. private static final long serialVersionUID = 1L;
  23. public GLY()
  24. {
  25. super("管理员登录");
  26. JPanel A=new JPanel();
  27. JPanel B=new JPanel();
  28. JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,A,B);//分屏的方式:左右HORIZONTAL_SPLIT,上下VERTICAL_SPLIT
  29. jSplitPane.setDividerLocation(150);
  30. this.add(jSplitPane);
  31. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. this.setSize(1020,700);
  33. this.setLocationRelativeTo(null);
  34. setVisible(true);
  35. //设置背景图片
  36. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十一.jpg");
  37. JLabel bgimg = new JLabel(img);
  38. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  39. bgimg.setOpaque(false);//背景照片透明
  40. //设置背景图片
  41. ImageIcon img1 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十二.jpg");
  42. JLabel bgimg1 = new JLabel(img1);
  43. bgimg1.setBounds(0,0,img1.getIconWidth(),img1.getIconHeight());
  44. bgimg1.setOpaque(false);//背景照片透明
  45. //生成A窗体中的各种组件
  46. A.setLayout(null);//自由布局
  47. JButton component1=new JButton("添加");component1.setBounds(20, 30, 70, 30);
  48. JButton component2=new JButton("修改");component2.setBounds(105, 30, 70, 30);
  49. JButton component3=new JButton("删除");component3.setBounds(20, 100, 70, 30);
  50. JButton component4=new JButton("退出");component4.setBounds(105, 100, 70, 30);//增删改退按钮
  51. JLabel component5=new JLabel("请输入学号:");component5.setBounds(395, 30, 100, 25);
  52. JTextField component6=new JTextField(20);component6.setBounds(500, 30, 200, 25);//后面的输入框
  53. JButton component7=new JButton("查询");component7.setBounds(715, 27, 80, 50);//查询按钮
  54. JButton component8=new JButton("全 部 信 息 展 示");component8.setBounds(460, 85, 230, 50);
  55. component1.setContentAreaFilled(false);component2.setContentAreaFilled(false);
  56. component3.setContentAreaFilled(false);component4.setContentAreaFilled(false);
  57. component7.setContentAreaFilled(false);component8.setContentAreaFilled(false);
  58. //把所有组件加入窗体对象中去
  59. A.add(component1);A.add(component2);A.add(component3);
  60. A.add(component4);A.add(component5);A.add(component6);
  61. A.add(component7);A.add(component8);A.add(bgimg);
  62. //四个表格
  63. String[][] datas = {};
  64. String[] titles = { "学号", "姓名","性别","年龄","专业" };
  65. String[][] datas1 = {};
  66. String[] titles1 = { "课程号", "课程名","学分"};
  67. String[][] datas2 = {};
  68. String[] titles2 = { "学号", "课程号","成绩","等级"};
  69. String[][] datas3 = {};
  70. String[] titles3 = { "账号", "密码","学号","电话号码"};
  71. DefaultTableModel myModel = new DefaultTableModel(datas, titles);// myModel存放表格的数据
  72. DefaultTableModel myModel1 = new DefaultTableModel(datas1, titles1);
  73. DefaultTableModel myModel2 = new DefaultTableModel(datas2, titles2);
  74. DefaultTableModel myModel3 = new DefaultTableModel(datas3, titles3);
  75. JTable table = new JTable(myModel);table.setPreferredScrollableViewportSize(new Dimension(700, 100));// 表格的显示尺寸// 表格对象table的数据来源是myModel对象
  76. JTable table1 = new JTable(myModel1);table1.setPreferredScrollableViewportSize(new Dimension(700, 100));
  77. JTable table2 = new JTable(myModel2);table2.setPreferredScrollableViewportSize(new Dimension(700, 100));
  78. JTable table3 = new JTable(myModel3);table3.setPreferredScrollableViewportSize(new Dimension(700, 100));
  79. // 产生一个带滚动条的面板
  80. JScrollPane scrollPane = new JScrollPane(table);scrollPane.setBounds(170, 110, 700, 100);
  81. JScrollPane scrollPane1 = new JScrollPane(table1);scrollPane1.setBounds(170, 220, 700, 100);
  82. JScrollPane scrollPane2 = new JScrollPane(table2);scrollPane2.setBounds(170, 330, 700, 100);
  83. JScrollPane scrollPane3 = new JScrollPane(table3);scrollPane3.setBounds(170, 0, 700, 100);
  84. // 将带滚动条的面板添加入B窗口中
  85. B.setLayout(null);//自由布局
  86. B.add(scrollPane);B.add(scrollPane1);B.add(scrollPane2);
  87. B.add(scrollPane3);B.add(bgimg1);
  88. component1.addActionListener(new ActionListener(){
  89. public void actionPerformed(ActionEvent e)
  90. {
  91. new Add();
  92. }
  93. });
  94. component2.addActionListener(new ActionListener(){
  95. public void actionPerformed(ActionEvent e)
  96. {
  97. new Update();
  98. }
  99. });
  100. component3.addActionListener(new ActionListener(){
  101. public void actionPerformed(ActionEvent e)
  102. {
  103. new Delete();
  104. }
  105. });
  106. component4.addActionListener(new ActionListener(){
  107. public void actionPerformed(ActionEvent e) {
  108. new Glogin();
  109. closeThis();
  110. }
  111. });
  112. component7.addActionListener(new ActionListener(){
  113. public void actionPerformed(ActionEvent e) {
  114. try {
  115. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  116. //加载对应的jdbc驱动
  117. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  118. //配置连接字符串
  119. String user="sa";//sa超级管理员
  120. String password="你的密码";//密码
  121. Connection conn=DriverManager.getConnection(url,user,password);
  122. //创建数据库连接对象
  123. Statement st=conn.createStatement();
  124. //创建SQL语句执行对象
  125. while(myModel.getRowCount()>0) {
  126. myModel.removeRow(myModel.getRowCount()-1);
  127. }
  128. while(myModel1.getRowCount()>0) {
  129. myModel1.removeRow(myModel1.getRowCount()-1);
  130. }
  131. while(myModel2.getRowCount()>0) {
  132. myModel2.removeRow(myModel2.getRowCount()-1);
  133. }
  134. while(myModel3.getRowCount()>0) {
  135. myModel3.removeRow(myModel3.getRowCount()-1);
  136. }//清空表上的东西
  137. String ID=component6.getText().trim();
  138. String s="(Select * from dbo.Student,dbo.PY where Student.Sno='"+ID+"' And Student.Sno=PY.Sno)";
  139. ResultSet r=st.executeQuery(s);
  140. while(r.next()) {
  141. Vector<String> ve = new Vector<String>();
  142. ve.addElement(r.getString(1));ve.addElement(r.getString(2));
  143. ve.addElement(r.getString(3));ve.addElement(r.getString(4));
  144. ve.addElement(r.getString(5));
  145. myModel.addRow(ve);
  146. Vector<String> ve1 = new Vector<String>();
  147. ve1.addElement(r.getString(6));ve1.addElement(r.getString(7));
  148. ve1.addElement(r.getString(8));ve1.addElement(r.getString(9));
  149. myModel3.addRow(ve1);
  150. }
  151. String s1="(Select * from dbo.SC,dbo.Course where Sno='"+ID+"' And SC.Cno=Course.Cno)";
  152. ResultSet r1=st.executeQuery(s1);
  153. while(r1.next()) {
  154. Vector<String> ve1 = new Vector<String>();
  155. ve1.addElement(r1.getString(1));ve1.addElement(r1.getString(2));
  156. ve1.addElement(r1.getString(3));ve1.addElement(r1.getString(4));
  157. myModel2.addRow(ve1);
  158. Vector<String> ve2 = new Vector<String>();
  159. ve2.addElement(r1.getString(5));ve2.addElement(r1.getString(6));
  160. ve2.addElement(r1.getString(8));
  161. myModel1.addRow(ve2);
  162. }
  163. conn.close();
  164. }catch (ClassNotFoundException ex) {
  165. System.out.println("没有找到对应的数据库驱动类");
  166. }
  167. catch (SQLException ex) {
  168. System.out.println("数据库连接或者是数据库操作失败");
  169. }
  170. }
  171. });
  172. component8.addActionListener(new ActionListener(){
  173. public void actionPerformed(ActionEvent e) {
  174. try {
  175. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  176. //加载对应的jdbc驱动
  177. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  178. //配置连接字符串
  179. String user="sa";//sa超级管理员
  180. String password="你的密码";//密码
  181. Connection conn=DriverManager.getConnection(url,user,password);
  182. //创建数据库连接对象
  183. Statement st=conn.createStatement();
  184. //创建SQL语句执行对象
  185. while(myModel.getRowCount()>0) {
  186. myModel.removeRow(myModel.getRowCount()-1);
  187. }
  188. while(myModel1.getRowCount()>0) {
  189. myModel1.removeRow(myModel1.getRowCount()-1);
  190. }
  191. while(myModel2.getRowCount()>0) {
  192. myModel2.removeRow(myModel2.getRowCount()-1);
  193. }
  194. while(myModel3.getRowCount()>0) {
  195. myModel3.removeRow(myModel3.getRowCount()-1);
  196. }
  197. String strSQL="(Select * from dbo.Student)";
  198. ResultSet rs=st.executeQuery(strSQL);
  199. while(rs.next()) {
  200. Vector<String> v = new Vector<String>();
  201. v.addElement(rs.getString(1));v.addElement(rs.getString(2));
  202. v.addElement(rs.getString(3));v.addElement(rs.getString(4));
  203. v.addElement(rs.getString(5));
  204. myModel.addRow(v);
  205. }
  206. String strSQL1="(Select * from dbo.Course)";
  207. ResultSet rs1=st.executeQuery(strSQL1);
  208. while(rs1.next()) {
  209. Vector<String> v1 = new Vector<String>();
  210. v1.addElement(rs1.getString(1));v1.addElement(rs1.getString(2));
  211. v1.addElement(rs1.getString(4));
  212. myModel1.addRow(v1);
  213. }
  214. String strSQL2="(Select * from dbo.SC)";
  215. ResultSet rs2=st.executeQuery(strSQL2);
  216. while(rs2.next()) {
  217. Vector<String> v2 = new Vector<String>();
  218. v2.addElement(rs2.getString(1));v2.addElement(rs2.getString(2));
  219. v2.addElement(rs2.getString(3));v2.addElement(rs2.getString(4));
  220. myModel2.addRow(v2);
  221. }
  222. String strSQL3="(Select * from dbo.PY)";
  223. ResultSet rs3=st.executeQuery(strSQL3);
  224. while(rs3.next()) {
  225. Vector<String> v3 = new Vector<String>();
  226. v3.addElement(rs3.getString(1));v3.addElement(rs3.getString(2));
  227. v3.addElement(rs3.getString(3));v3.addElement(rs3.getString(4));
  228. myModel3.addRow(v3);
  229. }
  230. conn.close();
  231. }catch (ClassNotFoundException ex) {
  232. System.out.println("没有找到对应的数据库驱动类");
  233. }
  234. catch (SQLException ex) {
  235. System.out.println("数据库连接或者是数据库操作失败");
  236. }
  237. }
  238. });
  239. }
  240. public void closeThis() {
  241. this.dispose();
  242. }
  243. }

4ee5d45f500645ad99d7bdb890196312.png

 增删改(Add,Update,Delete):

对于增删改这三部分的内容的话,其实还是比较简单的,所以,再次就不多说了,自己可以好好看看你,很容易理解!!

Add源代码:

  1. package 高校学生管理库;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import javax.swing.*;
  11. public class Add extends JFrame{
  12. private static final long serialVersionUID = 1L;
  13. final int WIDTH = 550;
  14. final int HEIGHT = 315;
  15. public Add() {
  16. super("添加信息");
  17. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  18. this.setSize(1020,700);
  19. this.setLocationRelativeTo(null);
  20. setVisible(true);
  21. //初始化字体
  22. font fonts = new font();
  23. //设置背景图片
  24. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图四.jpg");
  25. JLabel bgimg = new JLabel(img);
  26. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  27. bgimg.setOpaque(false);//背景照片透明
  28. //给盘子1创建对象
  29. javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
  30. jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
  31. jpanel_1.setLayout(null);//设置布局为空
  32. //给盘子2创建对象
  33. javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
  34. jpanel_2.setBounds(0,30,WIDTH,100);
  35. FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
  36. jpanel_2.setLayout(flowLayout);//设置布局为居中
  37. jpanel_2.setOpaque(false);//设置盘子为透明
  38. //给盘子3创建对象
  39. javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
  40. jpanel_3.setBounds(0,50,WIDTH,350);
  41. jpanel_3.setLayout(null);//设置布局为空
  42. jpanel_3.setOpaque(false);//设置盘子为透明
  43. JLabel title = new JLabel(" 添 加 界 面 ");
  44. title.setFont(font.title);
  45. title.setHorizontalAlignment(SwingConstants.CENTER);//居中
  46. JLabel j=new JLabel("学号:");JTextField c=new JTextField(15);//学号
  47. j.setBounds(40, 30, 50, 20); c.setBounds(120, 30, 100, 25);
  48. JLabel j1=new JLabel("姓名:");JTextField c1=new JTextField(15);//姓名
  49. j1.setBounds(40, 70, 50, 20);c1.setBounds(120, 70, 100, 25);
  50. JLabel j2=new JLabel("性别:");JTextField c2=new JTextField(15);//性别
  51. j2.setBounds(40, 110, 50, 30);c2.setBounds(120, 110, 100, 25);
  52. JLabel j3=new JLabel("年龄:");JTextField c3=new JTextField(15);//年龄
  53. j3.setBounds(40, 150, 50, 30);c3.setBounds(120, 150, 100, 25);
  54. JLabel j4=new JLabel("专业:");JTextField c4=new JTextField(15);//专业
  55. j4.setBounds(40, 190, 50, 30);c4.setBounds(120, 190, 100, 25);
  56. JLabel j5=new JLabel("课程:");JTextField c5=new JTextField(15);//课程
  57. j5.setBounds(40, 230, 50, 30);c5.setBounds(120, 230, 100, 25);
  58. JLabel j6=new JLabel("成绩:");JTextField c6=new JTextField(15);//成绩
  59. j6.setBounds(40, 270, 50, 30);c6.setBounds(120, 270, 100, 25);
  60. JLabel j7=new JLabel("等级:");JTextField c7=new JTextField(15);//等级
  61. j7.setBounds(40, 310, 50, 30); c7.setBounds(120, 310, 100, 25);
  62. JLabel j8=new JLabel("(温馨提醒:添加学生信息学号必填哦~,右边一列为课程信息)");j8.setBounds(10, 600, 500, 30);
  63. JLabel j11=new JLabel("课程号:");JTextField c11=new JTextField(15);//课程号
  64. j11.setBounds(300, 120, 70, 30); c11.setBounds(380, 120, 100, 25);
  65. JLabel j12=new JLabel("课程名:");JTextField c12=new JTextField(15);//课程名
  66. j12.setBounds(300, 170, 70, 30); c12.setBounds(380, 170, 100, 25);
  67. JLabel j13=new JLabel("学分:");JTextField c13=new JTextField(15);//学分
  68. j13.setBounds(300, 230, 70, 30); c13.setBounds(380, 230, 100, 25);
  69. JButton a=new JButton("确定");
  70. a.setContentAreaFilled(false);
  71. a.setBounds(100, 400, 100, 30);
  72. JButton b=new JButton("重置");
  73. b.setContentAreaFilled(false);
  74. b.setBounds(300, 400, 100, 30);
  75. jpanel_3.add(j); jpanel_3.add(j1); jpanel_3.add(j2);
  76. jpanel_3.add(j3); jpanel_3.add(j4); jpanel_3.add(j5);
  77. jpanel_3.add(j6); jpanel_3.add(j7); jpanel_3.add(j11);
  78. jpanel_3.add(j12);jpanel_3.add(j13);jpanel_1.add(j8);
  79. jpanel_3.add(c);jpanel_3.add(c1); jpanel_3.add(c2);
  80. jpanel_3.add(c3);jpanel_3.add(c4); jpanel_3.add(c5);
  81. jpanel_3.add(c6);jpanel_3.add(c7);jpanel_3.add(c11);
  82. jpanel_3.add(c12);jpanel_3.add(c13);jpanel_1.add(a);
  83. jpanel_1.add(b);this.add(jpanel_3);jpanel_2.add(title);
  84. this.add(jpanel_2);jpanel_1.add(bgimg);this.add(jpanel_1);
  85. a.addActionListener(new ActionListener(){
  86. public void actionPerformed(ActionEvent e) {
  87. try {
  88. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  89. //加载对应的jdbc驱动
  90. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  91. //配置连接字符串
  92. String user="sa";//sa超级管理员
  93. String password="你的密码";//密码
  94. Connection conn=DriverManager.getConnection(url,user,password);
  95. Statement st=conn.createStatement();
  96. String a=c.getText().trim(); String a1=c1.getText().trim();
  97. String a2=c3.getText().trim(); String a3=c5.getText().trim();
  98. String a4=c6.getText().trim(); String a11=c11.getText().trim();
  99. String a12=c12.getText().trim();String a13=c13.getText().trim();
  100. String a5=c2.getText().trim(); String a6=c4.getText().trim();
  101. String a7=c7.getText().trim();
  102. String s="(Select * from dbo.Student where Sno='"+a+"')";
  103. ResultSet r=st.executeQuery(s);
  104. if(r.next()) {
  105. JOptionPane.showMessageDialog(null, "该同学已存在哦~","提示消息",JOptionPane.WARNING_MESSAGE);
  106. }
  107. else {
  108. if(a.equals("")) {
  109. String s3="insert into dbo.Course(Cno,Cname,Ccredit) values('"+a11+"','"+a12+"','"+a13+"')";
  110. int r3=st.executeUpdate(s3);
  111. if(r3==1) {
  112. JOptionPane.showMessageDialog(null, "添加成功","提示消息",JOptionPane.WARNING_MESSAGE);
  113. }
  114. }
  115. else {
  116. String s1="insert into dbo.Student(Sno,Sname,Ssex,Sage,Sdept) values('"+a+"','"+a1+"','"+a5+"','"+a2+"','"+a6+"')";
  117. int r1=st.executeUpdate(s1);
  118. String s2="insert into dbo.SC(Sno,Cno,Grade,LEVEL) values('"+a+"','"+a3+"','"+a4+"','"+a7+"')";
  119. int r2=st.executeUpdate(s2);
  120. if(r1==1&&r2==1) {
  121. JOptionPane.showMessageDialog(null, "添加成功","提示消息",JOptionPane.WARNING_MESSAGE);
  122. }
  123. }
  124. }
  125. conn.close();
  126. }catch (ClassNotFoundException ex) {
  127. System.out.println("没有找到对应的数据库驱动类");
  128. }
  129. catch (SQLException ex) {
  130. System.out.println("数据库连接或者是数据库操作失败");
  131. }
  132. }
  133. });
  134. b.addActionListener(new ActionListener(){//重置清零
  135. public void actionPerformed(ActionEvent e) {
  136. c.setText("");c1.setText("");c2.setText("");
  137. c3.setText("");c4.setText("");c5.setText("");
  138. c6.setText("");c11.setText("");c12.setText("");
  139. c13.setText("");
  140. }
  141. });
  142. }
  143. }

5630e8e67903422abe5e04c102e4426e.png

 Delete源代码:

  1. package 高校学生管理库;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import javax.swing.*;
  11. public class Delete extends JFrame{
  12. private static final long serialVersionUID = 1L;
  13. final int WIDTH = 500;
  14. final int HEIGHT = 315;
  15. public Delete() {
  16. super("删除信息");
  17. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  18. this.setSize(1020,700);
  19. this.setLocationRelativeTo(null);
  20. setVisible(true);
  21. //初始化字体
  22. font fonts = new font();
  23. //设置背景图片
  24. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图五.jpg");
  25. JLabel bgimg = new JLabel(img);
  26. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  27. bgimg.setOpaque(false);//将面板设置为透明
  28. //给盘子1创建对象
  29. javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
  30. jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
  31. jpanel_1.setLayout(null);//设置布局为空
  32. jpanel_1.setOpaque(false);
  33. //给盘子2创建对象
  34. javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
  35. jpanel_2.setBounds(0,30,WIDTH,100);
  36. FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
  37. jpanel_2.setLayout(flowLayout);//设置布局为居中
  38. jpanel_2.setOpaque(false);//设置盘子为透明
  39. //给盘子3创建对象
  40. javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
  41. jpanel_3.setBounds(0,50,WIDTH,350);
  42. jpanel_3.setLayout(null);//设置布局为空
  43. jpanel_3.setOpaque(false);//设置盘子为透明
  44. JLabel title = new JLabel(" 删 除 界 面 ");
  45. title.setFont(font.title);
  46. title.setHorizontalAlignment(SwingConstants.CENTER);//居中
  47. JCheckBox optionA=new JCheckBox(" 姓名");optionA.setBounds(80, 70, 150, 20);
  48. JCheckBox optionB=new JCheckBox(" 性别");optionB.setBounds(80, 120, 150, 20);
  49. JCheckBox optionC=new JCheckBox(" 年龄");optionC.setBounds(80, 170, 150, 20);
  50. JCheckBox optionD=new JCheckBox(" 专业");optionD.setBounds(80, 220, 150, 20);
  51. JCheckBox optionE=new JCheckBox(" 成绩");optionE.setBounds(80, 320, 150, 20);
  52. JCheckBox optionF=new JCheckBox(" 等级");optionF.setBounds(80, 370, 150, 20);
  53. optionA.setContentAreaFilled(false);optionB.setContentAreaFilled(false);
  54. optionC.setContentAreaFilled(false);optionD.setContentAreaFilled(false);
  55. optionE.setContentAreaFilled(false);optionF.setContentAreaFilled(false);
  56. JLabel j=new JLabel("学号:");j.setBounds(30, 20, 50, 20);
  57. JLabel j1=new JLabel("(温馨提醒:谨慎删除哦~)");j1.setBounds(10, 600, 450, 15);
  58. JLabel j11=new JLabel("课程号:");j11.setBounds(300, 150, 70, 30);
  59. JLabel j12=new JLabel("课程名:");j12.setBounds(300, 250, 70, 30);
  60. JLabel cc=new JLabel("课程号:");cc.setBounds(20, 270, 70, 20);
  61. JTextField c=new JTextField(12);c.setBounds(90, 270, 150, 25);
  62. JTextField c1=new JTextField(12);c1.setBounds(90, 20, 150, 25);
  63. JTextField c11=new JTextField(10);c11.setBounds(380, 150, 100, 25);
  64. JTextField c12=new JTextField(10);c12.setBounds(380, 250, 100, 25);
  65. JButton a=new JButton("确定");a.setBounds(100, 400, 100, 30);
  66. a.setContentAreaFilled(false);
  67. JButton b=new JButton("重置");b.setBounds(300, 400, 100, 30);
  68. b.setContentAreaFilled(false);
  69. jpanel_1.add(a);jpanel_1.add(b);jpanel_3.add(j11);
  70. jpanel_3.add(c);jpanel_3.add(cc); jpanel_3.add(c12); jpanel_1.add(j1);
  71. jpanel_3.add(j);jpanel_3.add(c1);jpanel_3.add(c11); jpanel_3.add(j12);
  72. jpanel_3.add(optionA);jpanel_3.add(optionB);jpanel_3.add(optionC);
  73. jpanel_3.add(optionD);jpanel_3.add(optionE);jpanel_3.add(optionF);
  74. this.add(jpanel_3);jpanel_2.add(title);this.add(jpanel_2);
  75. jpanel_1.add(bgimg);this.add(jpanel_1);
  76. a.addActionListener(new ActionListener(){
  77. public void actionPerformed(ActionEvent e) {
  78. try {
  79. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  80. //加载对应的jdbc驱动
  81. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  82. //配置连接字符串
  83. String user="sa";//sa超级管理员
  84. String password="你的密码";//密码
  85. Connection conn=DriverManager.getConnection(url,user,password);
  86. //创建数据库连接对象
  87. Statement st=conn.createStatement();
  88. //创建SQL语句执行对象
  89. String a=c1.getText().trim();
  90. String a1=c.getText().trim();
  91. String s="(Select * from dbo.Student where Sno='"+a+"')";
  92. ResultSet r=st.executeQuery(s);
  93. if(r.next()) {
  94. if(optionA.isSelected()) {
  95. String strSQL="update dbo.Student set Sname="+null+" where Sno='"+a+"'";
  96. st.executeUpdate(strSQL);
  97. }
  98. if(optionB.isSelected()) {
  99. String strSQL="update dbo.Student set Ssex="+null+" where Sno='"+a+"'";
  100. st.executeUpdate(strSQL);
  101. }
  102. if(optionC.isSelected()) {
  103. String strSQL="update dbo.Student set Sage="+null+" where Sno='"+a+"'";
  104. st.executeUpdate(strSQL);
  105. }
  106. if(optionD.isSelected()) {
  107. String strSQL="update dbo.Student set Sdept="+null+" where Sno='"+a+"'";
  108. st.executeUpdate(strSQL);
  109. }
  110. if(optionE.isSelected()) {
  111. String strSQL="update dbo.SC set Grade="+null+" where Sno='"+a+"' And Cno='"+a1+"'";
  112. st.executeUpdate(strSQL);
  113. }
  114. if(optionF.isSelected()) {
  115. String strSQL="update dbo.SC set LEVEL="+null+" where Sno='"+a+"' And Cno='"+a1+"'";
  116. st.executeUpdate(strSQL);
  117. }
  118. JOptionPane.showMessageDialog(null,"删除成功哦~");
  119. }
  120. else {
  121. String cc=c11.getText().trim();
  122. String strSQL="delete from dbo.Course where Cno='"+cc+"' ";
  123. int rr=st.executeUpdate(strSQL);
  124. if(rr==1) {
  125. JOptionPane.showMessageDialog(null,"删除成功哦~");
  126. }
  127. else {
  128. JOptionPane.showMessageDialog(null,"课程不存在哦~");
  129. }
  130. }
  131. conn.close();
  132. }catch (ClassNotFoundException ex) {
  133. System.out.println("没有找到对应的数据库驱动类");
  134. }
  135. catch (SQLException ex) {
  136. System.out.println("数据库连接或者是数据库操作失败");
  137. }
  138. }
  139. });
  140. b.addActionListener(new ActionListener(){//重置清零
  141. public void actionPerformed(ActionEvent e) {
  142. c1.setText("");c11.setText("");c12.setText("");
  143. optionA.setSelected(false);optionB.setSelected(false);optionC.setSelected(false);
  144. optionD.setSelected(false);optionE.setSelected(false);optionF.setSelected(false);
  145. }
  146. });
  147. }
  148. }

da9edeb3ef404cf5a060c396eaec6797.png

Update源代码:

  1. package 高校学生管理库;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import javax.swing.*;
  11. public class Update extends JFrame{
  12. private static final long serialVersionUID = 1L;
  13. final int WIDTH = 550;
  14. final int HEIGHT = 315;
  15. public Update() {
  16. super("修改信息");
  17. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  18. this.setSize(1020,700);
  19. this.setLocationRelativeTo(null);
  20. setVisible(true);
  21. //初始化字体
  22. font fonts = new font();
  23. //设置背景图片
  24. ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图六.jpg");
  25. JLabel bgimg = new JLabel(img);
  26. bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
  27. bgimg.setOpaque(false);//将面板设置为透明
  28. //给盘子1创建对象
  29. javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
  30. jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
  31. jpanel_1.setLayout(null);//设置布局为空
  32. jpanel_1.setOpaque(false);//设置盘子为透明
  33. //给盘子2创建对象
  34. javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
  35. jpanel_2.setBounds(0,30,WIDTH,100);
  36. FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
  37. jpanel_2.setLayout(flowLayout);//设置布局为居中
  38. jpanel_2.setOpaque(false);//设置盘子为透明
  39. //给盘子3创建对象
  40. javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
  41. jpanel_3.setBounds(0,50,WIDTH,350);
  42. jpanel_3.setLayout(null);//设置布局为空
  43. jpanel_3.setOpaque(false);//设置盘子为透明
  44. JLabel title = new JLabel(" 修 改 界 面 ");
  45. title.setFont(font.title);
  46. title.setHorizontalAlignment(SwingConstants.CENTER);//居中
  47. JLabel j=new JLabel("学号:");j.setBounds(20, 30, 50, 20);
  48. JTextField c=new JTextField(15);c.setBounds(80, 30, 120, 25);//学号
  49. JLabel j1=new JLabel("姓名:");j1.setBounds(20, 70, 50, 20);
  50. JTextField c1=new JTextField(15);c1.setBounds(80, 70, 100, 25);//姓名
  51. JLabel j2=new JLabel("性别:");j2.setBounds(20, 110, 50, 30);
  52. JTextField c2=new JTextField(15);c2.setBounds(80, 110, 100, 25);//性别
  53. JLabel j3=new JLabel("年龄:");j3.setBounds(20, 150, 50, 30);
  54. JTextField c3=new JTextField(15);c3.setBounds(80, 150, 100, 25);//年龄
  55. JLabel j4=new JLabel("专业:");j4.setBounds(20, 190, 50, 30);
  56. JTextField c4=new JTextField(15);c4.setBounds(80, 190, 100, 25);//专业
  57. JLabel j5=new JLabel("课程:");j5.setBounds(20, 230, 50, 30);
  58. JTextField c5=new JTextField(20);c5.setBounds(80, 230, 100, 25);//课程
  59. JLabel j6=new JLabel("成绩:");j6.setBounds(20, 270, 50, 30);
  60. JTextField c6=new JTextField(20);c6.setBounds(80, 270, 100, 25);//成绩
  61. JLabel j7=new JLabel("等级:");j7.setBounds(20, 310, 50, 30);
  62. JTextField c7=new JTextField(15);c7.setBounds(80, 310, 100, 25);//等级
  63. JLabel j00=new JLabel("旧课程号:");j00.setBounds(290, 100, 90, 30);
  64. JTextField c00=new JTextField(15);c00.setBounds(380, 100, 100, 25);//旧课程号
  65. JLabel j11=new JLabel("新课程号:");j11.setBounds(290, 150, 90, 30);
  66. JTextField c11=new JTextField(15);c11.setBounds(380, 150, 100, 25);//新课程号
  67. JLabel j12=new JLabel("新课程名:");j12.setBounds(290, 200, 90, 30);
  68. JTextField c12=new JTextField(15);c12.setBounds(380, 200, 100, 25);//新课程名
  69. JLabel j13=new JLabel("新学分:");j13.setBounds(300, 250, 90, 30);
  70. JTextField c13=new JTextField(15);c13.setBounds(380, 250, 100, 25);//新学分
  71. JLabel j8=new JLabel("(温馨提醒:更新学生信息学号必填哦~右边一列为课程信息)");j8.setBounds(10, 600, 500, 15);
  72. JButton aa=new JButton("确定");aa.setBounds(100, 400, 100, 30);
  73. aa.setContentAreaFilled(false);
  74. JButton bb=new JButton("重置");bb.setBounds(300, 400, 100, 30);
  75. bb.setContentAreaFilled(false);
  76. jpanel_3.add(j); jpanel_3.add(j1);jpanel_3.add(j2);
  77. jpanel_3.add(j3);jpanel_3.add(j4);jpanel_3.add(j5);
  78. jpanel_3.add(j6);jpanel_3.add(j7);jpanel_3.add(j11);
  79. jpanel_3.add(j12);jpanel_3.add(j13);jpanel_3.add(c11);
  80. jpanel_3.add(c12);jpanel_3.add(c13);jpanel_3.add(c);
  81. jpanel_3.add(c1); jpanel_3.add(c2); jpanel_3.add(c3);
  82. jpanel_3.add(c4); jpanel_3.add(c5); jpanel_3.add(c6);
  83. jpanel_3.add(c7); jpanel_1.add(aa); jpanel_1.add(bb);
  84. jpanel_1.add(j8);jpanel_3.add(c00);jpanel_3.add(j00);
  85. this.add(jpanel_3);jpanel_2.add(title);this.add(jpanel_2);
  86. jpanel_1.add(bgimg);this.add(jpanel_1);
  87. aa.addActionListener(new ActionListener(){
  88. public void actionPerformed(ActionEvent e) {
  89. try {
  90. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  91. //加载对应的jdbc驱动
  92. String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
  93. //配置连接字符串
  94. String user="sa";//sa超级管理员
  95. String password="你的密码";//密码
  96. Connection conn=DriverManager.getConnection(url,user,password);
  97. Statement st=conn.createStatement();
  98. //修改过布局所以顺序有点乱=。=
  99. String a=c.getText().trim();String a1=c1.getText().trim();
  100. String a2=c3.getText().trim();String a3=c5.getText().trim();
  101. String a4=c6.getText().trim();String a11=c11.getText().trim();
  102. String a12=c12.getText().trim();String a13=c13.getText().trim();
  103. String a5=c2.getText().trim();String a6=c4.getText().trim();String a7=c7.getText().trim();
  104. String a00=c00.getText().trim();
  105. String s="(Select * from dbo.Student where Sno='"+a+"')";
  106. ResultSet r=st.executeQuery(s);
  107. if(r.next()) {
  108. if (!a1.isEmpty()) {
  109. String strSQL="update dbo.Student set Sname='"+a1+"' where Sno='"+a+"'";
  110. st.executeUpdate(strSQL);
  111. }
  112. if (!a2.isEmpty()) {
  113. String strSQL="update dbo.Student set Sage='"+a2+"' where Sno='"+a+"'";
  114. st.executeUpdate(strSQL);
  115. }
  116. if (!a5.isEmpty()) {
  117. String strSQL="update dbo.Student set Ssex='"+a5+"' where Sno='"+a+"'";
  118. st.executeUpdate(strSQL);
  119. }
  120. if (!a6.isEmpty()) {
  121. String strSQL="update dbo.Student set Sdept='"+a6+"' where Sno='"+a+"'";
  122. st.executeUpdate(strSQL);
  123. }
  124. if (!a3.isEmpty()) {
  125. String strSQL="update dbo.SC set Grade='"+a4+"' where Sno='"+a+"' And Cno='"+a3+"'" ;
  126. String strSQL1="update dbo.SC set LEVEL='"+a7+"' where Sno='"+a+"' And Cno='"+a3+"'" ;
  127. st.executeUpdate(strSQL);
  128. st.executeUpdate(strSQL1);
  129. }
  130. JOptionPane.showMessageDialog(null,"修改成功哦~");
  131. }
  132. else {
  133. String strSQL="update dbo.Course set Cno='"+a11+"',Cname='"+a12+"',Ccredit='"+a13+"' where Cno='"+a00+"'";
  134. int rs1=st.executeUpdate(strSQL);
  135. if(rs1==1) {
  136. JOptionPane.showMessageDialog(null,"修改成功哦~");
  137. }
  138. }
  139. conn.close();
  140. }catch (ClassNotFoundException ex) {
  141. System.out.println("没有找到对应的数据库驱动类");
  142. }
  143. catch (SQLException ex) {
  144. System.out.println("数据库连接或者是数据库操作失败");
  145. }
  146. }
  147. });
  148. //重置清零
  149. bb.addActionListener(new ActionListener(){
  150. public void actionPerformed(ActionEvent e) {
  151. c.setText("");c1.setText("");c2.setText("");
  152. c3.setText("");c4.setText("");c5.setText("");
  153. c6.setText("");c11.setText("");c12.setText("");
  154. c13.setText("");
  155. }
  156. });
  157. }
  158. }

246ca2e69ba24289bacebb31c90a174b.png

另外,对于这增删改的界面我单独设置了字体font类。

font类 :

  1. package 高校学生管理库;
  2. import java.awt.Font;
  3. public class font {
  4. public static Font title;
  5. public static Font account;
  6. public font() {
  7. title= new Font("宋体",Font.BOLD,30);
  8. account = new Font("宋体",Font.BOLD,16);
  9. }
  10. }

SQL语句:

还有用到的SQL语言:另外,管理员是需要自己设置的哦,你也可以去设置学生细腻

  1. --照片存放表
  2. create table image
  3. ( Sno char(10),
  4. ima varchar(50),
  5. );
  6. --学生表
  7. create table Student
  8. ( Sno char(9) primary key, /*列级完整性约束条件,Sno是主码 */
  9. Sname char(20) unique, /*Sname取唯一值 */
  10. Ssex char(2),
  11. Sage smallint,
  12. Sdept char(20)
  13. );
  14. --Course表
  15. create table Course
  16. ( Cno char(4) primary key,/* 列级完整性约束条件,Cno是主码*/
  17. Cname char(40) NOT NULL, /* 列级完整性约束条件,Cname不能取空值 */
  18. Cpno char(4),/* Cpno的含义是先修课 */
  19. Ccredit int,
  20. foreign key(Cpno) references Course(Cno) /*表级完整性约束条件,Cpno是外码,被参照表是Course,被参照列是Cno */
  21. );
  22. --信息表
  23. create table SC
  24. ( Sno char(9),
  25. Cno char(4),
  26. Grade int,
  27. LEVEL char(4),
  28. primary key(Sno,Cno),/*主码由两个属性构成,必须作为表级完整性进行定义 */
  29. foreign key(Sno) references Student(Sno), /*表级完整性约束条件,Sno是外码,被参照表是Student */
  30. foreign key(Cno) references Course(Cno)/*表级完整性约束条件,Cno是外码,被参照表是Course */
  31. );
  32. --管理员表
  33. create table GLY(
  34. ID1 varchar(30),
  35. PAWD1 varchar(50)--密码
  36. );
  37. --学生注册表
  38. create table PY(
  39. ID varchar(30),
  40. PAWD varchar(50),
  41. Sno char(9),
  42. phone char(20)
  43. );
  44. --学生信息
  45. --管理员
  46. --插入Course数据
  47. insert into Course values('1','数据库',null,4)
  48. insert into Course values('2','高等数学A',null,2)
  49. insert into Course values('3','信息系统',null,4)
  50. insert into Course values('4','操作系统',null,3)
  51. insert into Course values('5','数据结构','5',4)
  52. insert into Course values('6','计算机网络','5',4)
  53. insert into Course values('7','离散数学','5',2)
  54. insert into Course values('8','大学英语','5',4)
  55. insert into Course values('9','计算机组成原理','5',3)
  56. insert into Course values('10','算法设计与分析','5',4)
  57. insert into Course values('11','大学物理A',null,4)
  58. insert into Course values('12','高等数学B',null,2)
  59. insert into Course values('13','大学物理B',null,4)
  60. insert into Course values('14','计算机英语',null,3)
  61. insert into Course values('15','算法导论','5',4)
  62. insert into Course values('16','数据库原理与分析','5',4)
  63. insert into Course values('17','数据挖掘','5',2)
  64. insert into Course values('18','移动应用开发','5',4)
  65. insert into Course values('19','Javaweb开发应用','5',3)
  66. --课程等级

所需要用到的照片:

图一:

13d43d4d7cc34689be803aba18ece4fd.jpeg

图二:

 62520652936942d5b977e2d587dd1dae.jpeg

图三:

b6062c5da1b44951b513cd9e0ec66803.jpeg

图四:

df447c02029b4ff5ae83090e7a5c60a9.jpeg

 图五:

6c0a4cb215ed46d38b0e530f0ae32146.jpeg

图六:

7f8cfbe1ae3b405ca5691fe799f77f15.jpeg

图七:

46ede8ccb0e548118016a57b16c5c15f.jpeg

 图八:

22c4416170164e6287e665d2c1bb7841.jpeg

图九:

9288679a7d704d0cac253940d876d0b2.jpeg

 图十:

daf566bcc81d4a4b8b365e46d2698649.jpeg

图十一:

0bf09297d1784b6aac965cf30182a62c.jpeg

图十二:

5006e6bc22484d998baf6980911e6567.jpeg

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/871796
推荐阅读
相关标签
  

闽ICP备14008679号