赞
踩
主界面:我设置了全局字体,字体的话,如果在每个每个类中设置的话,会显得很臃肿,所以我就写了一方法定义了一个全局字体,当然,在最后面有些特殊需要的字体,我就另外重新定义了字体,而这个全局字体的话,是对所有的字体进行规范的。
然后添加了背景图片:
设置背景图片的方法基本上都是这样,但是你要知道,Java的Swing窗口,你在添加这些组件的时候,如果你没有设置透明,它是会覆盖的,从而显示不出来,这也是我之前所遇到的难题,所以后面每个类,再添加这些东西的时候,我都会去设置三个盘子,去容纳所需要添加的组件,如果你是是使用C#的话,那就会方便很多!
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图一.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- package 高校学生管理库;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Enumeration;
-
- import javax.swing.*;
- import javax.swing.plaf.FontUIResource;
-
- public class Interface extends JFrame{
- private static final long serialVersionUID = 1L;
- final int WIDTH = 500;
- final int HEIGHT = 315;
- // 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体
- private static void InitGlobalFont(Font font) {
- FontUIResource fontRes = new FontUIResource(font);
- for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
- Object key = keys.nextElement();
- Object value = UIManager.get(key);
- if (value instanceof FontUIResource) {
- UIManager.put(key, fontRes);
- }
- }
- }
- public Interface() {
- setTitle("登录");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- this.setSize(520,370);
- this.setLocationRelativeTo(null);
- setVisible(true);
-
- //给盘子1创建对象
- javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
- jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
- jpanel_1.setLayout(null);//设置布局为空
- //给盘子2创建对象
- javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
- jpanel_2.setBounds(0,30,WIDTH,100);
- FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
- jpanel_2.setLayout(flowLayout);//设置布局为居中
- jpanel_2.setOpaque(false);//设置盘子为透明
- //给盘子3创建对象
- javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
- jpanel_3.setBounds(0,50,WIDTH,350);
- jpanel_3.setLayout(null);//设置布局为空
- jpanel_3.setOpaque(false);//设置盘子为透明
- //初始化字体
- font fonts = new font();
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图一.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
-
- JLabel title = new JLabel(" 我 是 ");
- title.setFont(font.title);
- title.setHorizontalAlignment(SwingConstants.CENTER);//居中
-
- JButton g=new JButton("管理员");g.setBounds(200, 80, 100, 40);
- JButton x=new JButton("学生");x.setBounds(200, 200, 100, 40);
- g.setContentAreaFilled(false);x.setContentAreaFilled(false);
-
- jpanel_3.add(g);jpanel_3.add(x);jpanel_2.add(title);
- jpanel_1.add(bgimg);this.add(jpanel_3);this.add(jpanel_2);
- this.add(jpanel_1);
-
- x.addActionListener(new ActionListener() {//监听学生按钮
- public void actionPerformed(ActionEvent e) {
- new Slogin();
- }
- });
- g.addActionListener(new ActionListener() {//监听管理员按钮
- public void actionPerformed(ActionEvent e) {
- new Glogin();
- }
- });
- }
- public static void main(String[] args) {
- new Interface().setVisible(true);
- InitGlobalFont(new Font("alias", Font.PLAIN, 18)); //统一设置字体
- }
- }
这个登录界面的实现还是比较简单的,但是设置了验证码以及学生密码运用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)
- package 高校学生管理库;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- import java.sql.Statement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import javax.swing.*;
-
- public class Glogin extends JFrame{
- private static final long serialVersionUID = 1L;
- private Captcha vcode = new Captcha();
- JTextField co;
- //窗口常量
- final int WIDTH = 500;
- final int HEIGHT = 315;
- public Glogin() {
- super("管理员登陆");
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- this.setSize(520,370);
- this.setLocationRelativeTo(null);
- setVisible(true);
- //初始化字体
- font fonts = new font();
- //给盘子1创建对象
- javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
- jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
- jpanel_1.setLayout(null);//设置布局为空
- //给盘子2创建对象
- javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
- jpanel_2.setBounds(0,30,WIDTH,100);
- FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
- jpanel_2.setLayout(flowLayout);//设置布局为居中
- jpanel_2.setOpaque(false);//设置盘子为透明
- //给盘子3创建对象
- javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
- jpanel_3.setBounds(0,50,WIDTH,350);
- jpanel_3.setLayout(null);//设置布局为空
- jpanel_3.setOpaque(false);//设置盘子为透明
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图三.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- //登录界面
- JLabel title = new JLabel("管理员登录界面");
- title.setFont(font.title);
- JLabel l= new JLabel("账号");l.setBounds(80, 50, 60, 40);
- JLabel p= new JLabel("密码");p.setBounds(80, 100, 60, 40);
- JLabel code=new JLabel("验证码");code.setBounds(70, 150, 60, 40);
- JTextField user=new JTextField();user.setBounds(150, 50, 150, 30);
- JPasswordField pass=new JPasswordField();pass.setBounds(150, 100, 150, 30);
- co=new JTextField();co.setBounds(150, 150, 150, 30);
- JButton ok=new JButton("登录");ok.setBounds(180, 220, 70, 30);
- ok.setContentAreaFilled(false);vcode.setBounds(310, 145, 100, 40);
-
- jpanel_3.add(l);jpanel_3.add(p);jpanel_3.add(code);
- jpanel_3.add(user);jpanel_3.add(co);jpanel_3.add(vcode);
- jpanel_3.add(pass);jpanel_3.add(ok);jpanel_2.add(title);
- jpanel_1.add(bgimg);
- this.add(jpanel_3);this.add(jpanel_2);this.add(jpanel_1);
-
- ok.addActionListener(new ActionListener() {//监听登录按钮
- public void actionPerformed(ActionEvent e) {
- String jusername=user.getText();
- char s[]=pass.getPassword();
- String jpassword=new String(s);
- String coo=co.getText();
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- //创建数据库连接对象
- Statement st=conn.createStatement();
- //创建SQL语句执行对象
- String strSQL="(Select * from dbo.GLY where ID1='"+jusername+"'And PAWD1='"+jpassword+"' )";
- ResultSet rs=st.executeQuery(strSQL);
- if(coo.isEmpty()) {
- JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- else {
- if(!isValidCodeRight()) {
- JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- else {
- if(rs.next()) {
- new GLY();
- closeThis();
- }
- else {
- JOptionPane.showMessageDialog(null,"用户名不存在或密码错误","错误!",JOptionPane.ERROR_MESSAGE);
- }
- conn.close();//关闭数据库连接
- }
- }
- }
- catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- }
- public boolean isValidCodeRight() {
- if(co == null) {
- return false;
- }else if(vcode == null) {
- return true;
- }else if(vcode.getCode() .equals(co.getText())) {
- return true;
- }else
- return false;
- }
- public void closeThis() {//关闭当前界面
- this.dispose();
- }
- }
- package 高校学生管理库;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.UnsupportedEncodingException;
- import java.security.NoSuchAlgorithmException;
- import java.sql.Statement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import javax.swing.*;
-
- public class Slogin extends JFrame {
- private static final long serialVersionUID = 1L;
- private Captcha vcode = new Captcha();
- JTextField co;
- static JTextField user;
- //窗口常量
- final int WIDTH = 500;
- final int HEIGHT = 315;
- public Slogin() {
- super("学生登陆");
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- this.setSize(520,370);
- this.setLocationRelativeTo(null);
- setVisible(true);
- //初始化字体
- font fonts = new font();
- //给盘子1创建对象
- javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
- jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
- jpanel_1.setLayout(null);//设置布局为空
- //给盘子2创建对象
- javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
- jpanel_2.setBounds(0,30,WIDTH,100);
- FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);//设置布局为居中
- jpanel_2.setLayout(flowLayout);
- jpanel_2.setOpaque(false);//设置盘子为透明
- //给盘子3创建对象
- javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
- jpanel_3.setBounds(0,50,WIDTH,350);
- jpanel_3.setLayout(null);//设置布局为空
- jpanel_3.setOpaque(false);//设置盘子为透明
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图二.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- //登录界面
- JLabel title = new JLabel("学生登录界面");
- title.setFont(font.title);
- JLabel l= new JLabel("账号");l.setBounds(80, 50, 60, 40);
- JLabel p= new JLabel("密码");p.setBounds(80, 100, 60, 40);
- JLabel code=new JLabel("验证码");code.setBounds(70, 150, 60, 40);
- user=new JTextField();user.setBounds(150, 50, 150, 30);
- JPasswordField pass=new JPasswordField();pass.setBounds(150, 100, 150, 30);
- co=new JTextField();co.setBounds(150, 150, 150, 30);
- JButton ok=new JButton("登录"); ok.setBounds(120, 220, 70, 30);
- ok.setContentAreaFilled(false);
- JButton register=new JButton("注册");register.setBounds(250, 220, 70, 30);
- register.setContentAreaFilled(false);
- vcode.setBounds(310, 145, 100, 40);
-
- jpanel_3.add(l);jpanel_3.add(p);jpanel_3.add(code);
- jpanel_3.add(user);jpanel_3.add(co);jpanel_3.add(vcode);
- jpanel_3.add(register);jpanel_3.add(pass);jpanel_3.add(ok);
- jpanel_2.add(title);jpanel_1.add(bgimg);
- this.add(jpanel_3);this.add(jpanel_2);this.add(jpanel_1);
-
- register.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- new Enroll();
- closeThis();
- }
- });
- ok.addActionListener(new ActionListener() { //监听登录按钮
- public void actionPerformed(ActionEvent e) {
- String jusername=user.getText();
- char s[]=pass.getPassword();
- String jpassword=new String(s);
- String coo=co.getText();
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- //创建数据库连接对象
- Statement st=conn.createStatement();
- //创建SQL语句执行对象
-
- Md5 md5 = new Md5();//使用MD5加密
- String newString = md5.EncoderByMd5(jpassword);
- String strSQL="(Select * from dbo.PY where ID='"+jusername+"'And PAWD='"+newString+"' )";
- ResultSet rs=st.executeQuery(strSQL);
- if(coo.isEmpty()) {
- JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- else {
- if(!isValidCodeRight()) {
- JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- else {
- if(rs.next()) {
- new Student();
- closeThis();
- }
- else {
- JOptionPane.showMessageDialog(null,"用户名不存在或密码错误","错误!",JOptionPane.ERROR_MESSAGE);
- }
- conn.close();//关闭数据库连接
- }
- }
- }
- catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }catch (NoSuchAlgorithmException e1) {
- e1.printStackTrace();
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- }
- }
- });
- }
- public boolean isValidCodeRight() {
- if(co == null) {
- return false;
- }else if(vcode == null) {
- return true;
- }else if(vcode.getCode() .equals(co.getText())) {
- return true;
- }else
- return false;
- }
- public void closeThis() {//关闭当前界面
- this.dispose();
- }
- }
- package 高校学生管理库;
-
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import sun.misc.BASE64Encoder;
-
- public class Md5 { /**利用MD5进行加密*/
- public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
- //确定计算方法
- MessageDigest md5=MessageDigest.getInstance("MD5");
- BASE64Encoder base64en = new BASE64Encoder();
- //加密后的字符串
- String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
- return newstr;
- }
- }
- package 高校学生管理库;
-
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
-
- import java.util.Random;
-
- import javax.swing.JComponent;
-
- public class Captcha extends JComponent implements MouseListener {
- private static final long serialVersionUID = 1L;
- private String code;
- private int width, height = 40;
- private int codeLength = 4;
- private Random random = new Random();
- public Captcha() {
- width = this.codeLength * 16 + (this.codeLength - 1) * 10;
- setPreferredSize(new Dimension(width, height));
- setSize(width, height);
- this.addMouseListener(this);
- setToolTipText("点击可以更换验证码");
- }
- public int getCodeLength() {
- return codeLength;
- }
- //设置验证码文字的长度
- public void setCodeLength(int codeLength) {
- if(codeLength < 4) {
- this.codeLength = 4;
- } else {
- this.codeLength = codeLength;
- }
- }
- public String getCode() {
- return code;
- }
- //产生随机的颜色
- public Color getRandColor(int min, int max) {
- if (min > 255)
- min = 255;
- if (max > 255)
- max = 255;
- int red = random.nextInt(max - min) + min;
- int green = random.nextInt(max - min) + min;
- int blue = random.nextInt(max - min) + min;
- return new Color(red, green, blue);
- }
- //设置验证码具体的字母是什么
- protected String generateCode() {
- char[] codes = new char[this.codeLength];
- for (int i = 0, len = codes.length; i < len; i++) {
- if (random.nextBoolean()) {
- codes[i] = (char) (random.nextInt(26) + 65);
- } else {
- codes[i] = (char) (random.nextInt(26) + 97);
- }
- }
- this.code = new String(codes);
- return this.code;
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- if(this.code == null || this.code.length() != this.codeLength) {
- this.code = generateCode();
- }
- width = this.codeLength * 16 + (this.codeLength - 1) * 10;
- super.setSize(width, height);
- super.setPreferredSize(new Dimension(width, height));
- Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
- g.setFont(mFont);
- //绘制出验证码的背景的矩形轮廓
- Graphics2D g2d = (Graphics2D) g;
- g2d.setColor(getRandColor(200, 250));
- g2d.fillRect(0, 0, width, height);
- g2d.setColor(getRandColor(180, 200));
- g2d.drawRect(0, 0, width - 1, height - 1);
- //绘制出验证码背景的线
- int i = 0, len = 150;
- for (; i < len; i++) {
- int x = random.nextInt(width - 1);
- int y = random.nextInt(height - 1);
- int x1 = random.nextInt(width - 10) + 10;
- int y1 = random.nextInt(height - 4) + 4;
- g2d.setColor(getRandColor(180, 200));
- g2d.drawLine(x, y, x1, y1);
- }
- //绘制出验证码的具体字母
- i = 0; len = this.codeLength;
- FontMetrics fm = g2d.getFontMetrics();
- int base = (height - fm.getHeight())/2 + fm.getAscent();
- for(;i<len;i++) {
- int b = random.nextBoolean() ? 1 : -1;
- g2d.rotate(random.nextInt(10)*0.01*b);
- g2d.setColor(getRandColor(20, 130));
- g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);
- }
- }
- //下一个验证码
- public void nextCode() {
- generateCode();
- repaint();
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- nextCode();
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- }
学生注册界面:
上传照片功能,这里我遇到了一个问题就是关于转义字符的问题,因为我要获取选中的图片的路径并将照片添加到JLabel里展示出来,当我们需要在字符串中使用普通的反斜杠时,就需要使用双反斜杠\\来表示;java编译器会将\\\\ 解释为 \\ ,而正则表达式里,双斜杠 \\ 代表单斜杠 \ ,这也就是为什么要用 \\\\ 表示 \ ,\\\\\\\\表示\\。
- pho.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser fileChooser = new JFileChooser("C:\\Users\\god36c\\OneDrive\\桌面");
- fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
- int returnVal = fileChooser.showOpenDialog(fileChooser);
- if(returnVal == JFileChooser.APPROVE_OPTION) {
- File filePath = fileChooser.getSelectedFile();//获取图片路径
- System.out.println(filePath);
- String f=filePath.getPath();
- String filePath1=f.replaceAll("\\\\\\\\", "\\\\\\\\"); //将\转义为\\
- ImageIcon p = new ImageIcon(filePath1);
- photo.setIcon(p);
- }
- }
- });
然后这里用到了数据库中的表来判断是否已经存在这个人,如果存在注册失败,不存在并信息填入符合要求则成功(照片则保存照片的路径)。
- package 高校学生管理库;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- import java.io.File;
- import java.io.UnsupportedEncodingException;
- import java.security.NoSuchAlgorithmException;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- import javax.swing.*;
-
- public class Enroll extends JFrame{
- private static final long serialVersionUID = 1L;
- JTextField co;
- private Captcha vcode = new Captcha();
- final int WIDTH = 1020;
- final int HEIGHT = 1615;
- public Enroll() {
- super("注册");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(1020,700);
- this.setLocationRelativeTo(null); //此窗口将置于屏幕的中央
- setVisible(true);
-
- //初始化字体
- font fonts = new font();
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图七.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- bgimg.setOpaque(false);//背景照片透明
- //给盘子1创建对象
- javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
- jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
- jpanel_1.setLayout(null);//设置布局为空
- jpanel_1.setOpaque(false);
- //给盘子2创建对象
- javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
- jpanel_2.setBounds(0,30,WIDTH,100);
- FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
- jpanel_2.setLayout(flowLayout);//设置布局为居中
- jpanel_2.setOpaque(false);//设置盘子为透明
- //给盘子3创建对象
- javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
- jpanel_3.setBounds(0,50,WIDTH,350);
- jpanel_3.setLayout(null);//设置布局为空
- jpanel_3.setOpaque(false);//设置盘子为透明
-
- JLabel title = new JLabel(" 注 册 界 面 ");
- title.setFont(font.title);
- title.setHorizontalAlignment(SwingConstants.CENTER);//居中
-
- JLabel username=new JLabel("账号");username.setBounds(58, 46, 60, 40);
- JLabel password=new JLabel("密码");password.setBounds(54,100,120,30);
- JLabel id=new JLabel("学号");id.setBounds(54,150,120,30);
- JLabel phone=new JLabel("手机号码");phone.setBounds(22,200,120,30);
- JLabel code=new JLabel("验证码");code.setBounds(41,250,120,30);
- JLabel photo=new JLabel();photo.setBounds(280,100,200,200);
- JTextField user=new JTextField();user.setBounds(100,50,120,30);
- JTextField pass=new JTextField(); pass.setBounds(100,100,120,30);
- JTextField idd=new JTextField();idd.setBounds(100,150,120,30);
- JTextField ph=new JTextField();ph.setBounds(100,200,120,30);
- co=new JTextField();co.setBounds(100,250,120,30);
- JButton pho=new JButton("上传照片");pho.setBounds(305,280,100,30);
- JButton register=new JButton("注 册"); register.setBounds(150,380,80,30);
- JButton exit=new JButton("退 出");exit.setBounds(250,380,80,30);
- pho.setContentAreaFilled(false);register.setContentAreaFilled(false);exit.setContentAreaFilled(false);
- vcode.setBounds(112, 300, 100, 40);
-
- jpanel_1.add(username);jpanel_1.add(user);jpanel_1.add(password);
- jpanel_1.add(pass);jpanel_1.add(id);jpanel_1.add(idd);
- jpanel_1.add(phone);jpanel_1.add(ph);jpanel_1.add(code);
- jpanel_1.add(co);jpanel_1.add(photo);jpanel_1.add(pho);
- jpanel_1.add(register);jpanel_1.add(exit);jpanel_1.add(vcode);
- jpanel_1.add(bgimg);this.add(jpanel_1);jpanel_2.add(title);
- this.add(jpanel_2);this.add(jpanel_3);
-
- pho.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser fileChooser = new JFileChooser("C:\\Users\\god36c\\OneDrive\\桌面");
- fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
- int returnVal = fileChooser.showOpenDialog(fileChooser);
- if(returnVal == JFileChooser.APPROVE_OPTION) {
- File filePath = fileChooser.getSelectedFile();//获取图片路径
- System.out.println(filePath);
- String f=filePath.getPath();
- String filePath1=f.replaceAll("\\\\\\\\", "\\\\\\\\"); //将\转义为\\
- ImageIcon p = new ImageIcon(filePath1);
- photo.setIcon(p);
- }
- }
- });
- register.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String user1=user.getText().trim();
- String pass1=pass.getText().trim();
- String id1=idd.getText().trim();
- String ph1=ph.getText().trim();
- String co1=co.getText();
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- //创建数据库连接对象
- Statement st=conn.createStatement();
- //创建SQL语句执行对象
- String strSQL="(Select * from dbo.PY where ID='"+user1+"' )";
- ResultSet rs=st.executeQuery(strSQL);
- Md5 md5 = new Md5();
- String newString = md5.EncoderByMd5(pass1);
- if(co1.isEmpty()) {
- JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- else {
- if(!isValidCodeRight()) {
- JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- else {
- if(rs.next()) {
- JOptionPane.showMessageDialog(null,"用户名已存在","错误!", JOptionPane.ERROR_MESSAGE);
- }
- else {
- String sql = "insert into dbo.PY(ID,PAWD,Sno,phone) values('"+user1+"','"+newString+"','"+id1+"','"+ph1+"')";
- PreparedStatement pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
- pst.executeUpdate();
- pst.close();
- JOptionPane.showMessageDialog(null,"注册成功");
- }
- conn.close();//关闭数据库连接
- }
- }
- }
- catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- catch (NoSuchAlgorithmException e1) {
- e1.printStackTrace();
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- }
- }
- });
- exit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- closeThis();
- new Slogin();
- }
- });
- }
- public boolean isValidCodeRight() {
- if(co == null) {
- return false;
- }else if(vcode == null) {
- return true;
- }else if(vcode.getCode() .equals(co.getText())) {
- return true;
- }else
- return false;
- }
- public void closeThis(){//关闭当前界面
- this.dispose();
- }
- }
另外,大家要注意数据库链接的时候,我的数据库是和你们不一样的,所以你们要留意一下,自己的数据库是否和我一样,如果不一样要根据自己的数据库去更改一下,不然是链接不成功的!!接下来的学生和管理员类,是这次系统设计的核心。
- package 高校学生管理库;
-
- import java.awt.Dimension;
- import java.awt.Menu;
- import java.awt.MenuBar;
- import java.awt.MenuItem;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.Statement;
- import java.util.Vector;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTabbedPane;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import javax.swing.table.DefaultTableModel;
-
- public class Student extends JFrame{
- private static final long serialVersionUID = 1L;
- public Student() throws SQLException, ClassNotFoundException
- {
- super("学生登陆");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(800,600);
- this.setLocationRelativeTo(null);
- setVisible(true);
-
- MenuBar bar = new MenuBar();// 创建菜单栏
- Menu fileMenu = new Menu("FILE");// 创建“文件”菜单
- MenuItem open = new MenuItem("OPEN");
- MenuItem exit = new MenuItem("EXIT");
- Menu help = new Menu("HELP");// 创建“帮助"菜单
- MenuItem print = new MenuItem("PRINT");
-
- exit.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- new Slogin();
- closeThis();
- }
- });
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图八.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- bgimg.setOpaque(false);//背景照片透明
- //设置背景图片
- ImageIcon img1 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十.jpg");
- JLabel bgimg1 = new JLabel(img1);
- bgimg1.setBounds(0,0,img1.getIconWidth(),img1.getIconHeight());
- bgimg1.setOpaque(false);//背景照片透明
- //设置背景图片
- ImageIcon img2 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图九.jpg");
- JLabel bgimg2 = new JLabel(img2);
- bgimg2.setBounds(0,0,img2.getIconWidth(),img2.getIconHeight());
- bgimg2.setOpaque(false);//背景照片透明
-
- fileMenu.add(print);fileMenu.add(open);
- fileMenu.addSeparator();// 设置菜单分隔符
- fileMenu.add(exit);
- bar.add(fileMenu);// 将文件添加到菜单栏上
- bar.add(help);// 将文件添加到菜单栏上
- setMenuBar(bar);// 设置菜单栏,使用这种方式设置菜单栏可以不占用布局空间
- //创建组件
- JPanel jp1= new JPanel();//定义面板
- JPanel jp2= new JPanel();
- JPanel jp3= new JPanel();
- jp1.setLayout(null);//自由布局
- jp2.setLayout(null);//自由布局
- jp3.setLayout(null);//自由布局
- //jp1面板上上的内容
- String[][] datas = {};
- String[] titles = { "学号", "姓名","性别","年龄","专业" };
- String[][] datas1 = {};
- String[] titles1 = { "课程号", "分数","等级"};
-
- DefaultTableModel myModel = new DefaultTableModel(datas, titles);// myModel存放表格的数据
- DefaultTableModel myModel1 = new DefaultTableModel(datas1, titles1);
- JTable table = new JTable(myModel);// 表格对象table的数据来源是myModel对象
- JTable table1 = new JTable(myModel1);
- table.setPreferredScrollableViewportSize(new Dimension(550, 100));// 表格的显示尺寸
- table1.setPreferredScrollableViewportSize(new Dimension(550, 100));
- // 产生一个带滚动条的面板
- JScrollPane scrollPane = new JScrollPane(table);scrollPane.setBounds(50, 190, 550, 70);
- JScrollPane scrollPane1 = new JScrollPane(table1);scrollPane1.setBounds(50, 290, 550, 100);
- //行高
- table.setRowHeight(20);
- table1.setRowHeight(20);
-
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="123456";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- //创建数据库连接对象
- Statement st=conn.createStatement();
- //创建SQL语句执行对象
-
- String strSQL="(Select * from dbo.Student where Sname='"+ Slogin.user.getText()+"')";
- ResultSet rs=st.executeQuery(strSQL);
- if(rs.next()) {
- Vector<String> ve = new Vector<String>();
- ve.addElement(rs.getString(1));ve.addElement(rs.getString(2));
- ve.addElement(rs.getString(3));ve.addElement(rs.getString(4));
- ve.addElement(rs.getString(5));
- myModel.addRow(ve);
- }
- String s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Student.Sno=SC.Sno)";
- ResultSet r1=st.executeQuery(s1);
- while(r1.next()) {
- Vector<String> ve1 = new Vector<String>();
- ve1.addElement(r1.getString(7));
- ve1.addElement(r1.getString(8));
- ve1.addElement(r1.getString(9));
- myModel1.addRow(ve1);
- }
- JButton again=new JButton("刷 新~");again.setBounds(490, 140, 80, 30);
- again.setContentAreaFilled(false);
- again.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- Statement st=conn.createStatement();
- while(myModel1.getRowCount()>0) {
- myModel1.removeRow(myModel1.getRowCount()-1);
- }
- String s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Student.Sno=SC.Sno)";
- ResultSet r1=st.executeQuery(s1);
- while(r1.next()) {
- Vector<String> ve1 = new Vector<String>();
- ve1.addElement(r1.getString(7));
- ve1.addElement(r1.getString(8));
- ve1.addElement(r1.getString(9));
- myModel1.addRow(ve1);
- }
- conn.close();
- }catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- //jp2面板上的内容
- String[][] datas2 = {};
- String[] titles2 = { "课程号", "课程名","学分" };
- DefaultTableModel myModel2 = new DefaultTableModel(datas2, titles2);
- JTable table2 = new JTable(myModel2);
- table2.setRowHeight(20);
- table2.setPreferredScrollableViewportSize(new Dimension(550, 400));
- JScrollPane scrollPane2 = new JScrollPane(table2);scrollPane2.setBounds(50, 20, 550, 400);
- String s2="(Select * from dbo.Course)";
- ResultSet r2=st.executeQuery(s2);
- while(r2.next()) {
- Vector<String> ve2 = new Vector<String>();
- ve2.addElement(r2.getString(1));
- ve2.addElement(r2.getString(2));
- ve2.addElement(r2.getString(4));
- myModel2.addRow(ve2);
- }
- conn.close();
- //jp2上的内容
- JLabel a=new JLabel("请输入你想选的课的课程号:");a.setBounds(50, 470, 270, 30);
- JTextField b=new JTextField(20);b.setBounds(320, 470, 150, 25);
- JButton c=new JButton("确定");c.setBounds(500, 470, 80, 27);
- //jp3上的内容
- JLabel ja1=new JLabel("你想查询的科目是:");ja1.setBounds(50, 50, 200, 30);
- JLabel ja2=new JLabel("你的成绩是:");ja2.setBounds(80, 220, 150, 30);
- JLabel ja3=new JLabel("你的等级是:");ja3.setBounds(80, 270, 150, 30);
- JLabel ja4=new JLabel("(输入课程号哦~)");ja4.setBounds(255, 80, 150, 30);
- JTextField b1=new JTextField(15);b1.setBounds(260, 50, 150, 25);
- JTextField b2=new JTextField(15);b2.setBounds(260, 220, 100, 25);
- JTextField b3=new JTextField(15);b3.setBounds(260, 270, 100, 25);
- JButton c1=new JButton("查 询");c1.setBounds(450, 50, 70, 30);
- // 将组件添加入jp1窗口中
- jp1.add(scrollPane);jp1.add(scrollPane1);
- jp1.add(again);jp1.add(bgimg);
- // 将组件添加入jp2窗口中
- jp2.add(scrollPane2);jp2.add(a);jp2.add(b);
- jp2.add(c);jp2.add(bgimg1);
- // 将组件添加入jp3窗口中
- jp3.add(ja1);jp3.add(ja2);jp3.add(ja3);
- jp3.add(ja4);jp3.add(b1);jp3.add(b2);
- jp3.add(b3);jp3.add(c1);jp3.add(bgimg2);
- JTabbedPane jtbp=new JTabbedPane(JTabbedPane.LEFT); //创建选项卡并使选项卡垂直排列
- jtbp.add("个人信息",jp1);jtbp.add("选课",jp2);jtbp.add("成绩查询",jp3);
- this.add(jtbp); //添加选项卡窗格到容器
- c.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- Statement st=conn.createStatement();
- String ok=b.getText().trim();
- String s="(Select * from dbo.Course where Cno='"+ok+"' )";
- ResultSet r=st.executeQuery(s);
- if(r.next()) {
- String s1="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And SC.Sno=Student.Sno )";
- ResultSet r1=st.executeQuery(s1);
- if(r1.next()) {
- String s2="(Select * from dbo.SC where Sno='"+r1.getString(1)+"' And Cno='"+ok+"' )";
- ResultSet r2=st.executeQuery(s2);
- if(r2.next()) {
- JOptionPane.showMessageDialog(null, "你已经选过该科目了~","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- else {
- String ss="(Select * from dbo.Student where Sname='"+ Slogin.user.getText()+"')";
- ResultSet rr=st.executeQuery(ss);
- if(rr.next()) {
- String strSQL="insert into dbo.SC(Sno,Cno) values('"+rr.getString(1)+"','"+ok+"')";
- int rr1=st.executeUpdate(strSQL);
- if(rr1==1) {
- JOptionPane.showMessageDialog(null, "选课成功","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- }
- }
- }
- }
- else {
- JOptionPane.showMessageDialog(null, "没有这种科目哦~","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- conn.close();
- }catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- c1.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- Statement st=conn.createStatement();
-
- String B1=b1.getText().trim();
- String L="(Select * from dbo.Student,dbo.SC where Sname='"+ Slogin.user.getText()+"' And Cno='"+B1+"' And Student.Sno=SC.Sno )";
- ResultSet M=st.executeQuery(L);
- if(M.next()) {
- b2.setText(M.getString(8));
- b3.setText(M.getString(9));
- }
- else {
- JOptionPane.showMessageDialog(null, "没有该科目的成绩哦~","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- conn.close();
- }catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- }
- public void closeThis() {//关闭当前界面
- this.dispose();
- }
- }
这个管理员类,主要是显示和查询功能,当然,管理员这上面还是有一些组件,增删改的跳转,以及我们可以全部查询,也可以按学生学号进行查询,以及显示各种,专业,但是要记住的是,如果你没有注册学生账号,那么你按学号查询的话是只能查询到,所选的课程和课程号等,是不能查询到学生的其他信息的,简单来说就是,你只能查询到,最下面两组的信息,但是,如果你是按我的代码去写的话,那就是不会有任何问题,但是你如果是自己去添加什么功能的话,那么你的数据库列哪里就会发生改变,你就自己去调整和更改那些列,不然,是显示不出来的!!
- package 高校学生管理库;
-
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Vector;
-
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JSplitPane;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import javax.swing.table.DefaultTableModel;
-
- public class GLY extends JFrame {
- private static final long serialVersionUID = 1L;
- public GLY()
- {
- super("管理员登录");
- JPanel A=new JPanel();
- JPanel B=new JPanel();
- JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,A,B);//分屏的方式:左右HORIZONTAL_SPLIT,上下VERTICAL_SPLIT
- jSplitPane.setDividerLocation(150);
- this.add(jSplitPane);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(1020,700);
- this.setLocationRelativeTo(null);
- setVisible(true);
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十一.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- bgimg.setOpaque(false);//背景照片透明
- //设置背景图片
- ImageIcon img1 = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图十二.jpg");
- JLabel bgimg1 = new JLabel(img1);
- bgimg1.setBounds(0,0,img1.getIconWidth(),img1.getIconHeight());
- bgimg1.setOpaque(false);//背景照片透明
- //生成A窗体中的各种组件
- A.setLayout(null);//自由布局
- JButton component1=new JButton("添加");component1.setBounds(20, 30, 70, 30);
- JButton component2=new JButton("修改");component2.setBounds(105, 30, 70, 30);
- JButton component3=new JButton("删除");component3.setBounds(20, 100, 70, 30);
- JButton component4=new JButton("退出");component4.setBounds(105, 100, 70, 30);//增删改退按钮
- JLabel component5=new JLabel("请输入学号:");component5.setBounds(395, 30, 100, 25);
- JTextField component6=new JTextField(20);component6.setBounds(500, 30, 200, 25);//后面的输入框
- JButton component7=new JButton("查询");component7.setBounds(715, 27, 80, 50);//查询按钮
- JButton component8=new JButton("全 部 信 息 展 示");component8.setBounds(460, 85, 230, 50);
- component1.setContentAreaFilled(false);component2.setContentAreaFilled(false);
- component3.setContentAreaFilled(false);component4.setContentAreaFilled(false);
- component7.setContentAreaFilled(false);component8.setContentAreaFilled(false);
- //把所有组件加入窗体对象中去
- A.add(component1);A.add(component2);A.add(component3);
- A.add(component4);A.add(component5);A.add(component6);
- A.add(component7);A.add(component8);A.add(bgimg);
- //四个表格
- String[][] datas = {};
- String[] titles = { "学号", "姓名","性别","年龄","专业" };
- String[][] datas1 = {};
- String[] titles1 = { "课程号", "课程名","学分"};
- String[][] datas2 = {};
- String[] titles2 = { "学号", "课程号","成绩","等级"};
- String[][] datas3 = {};
- String[] titles3 = { "账号", "密码","学号","电话号码"};
- DefaultTableModel myModel = new DefaultTableModel(datas, titles);// myModel存放表格的数据
- DefaultTableModel myModel1 = new DefaultTableModel(datas1, titles1);
- DefaultTableModel myModel2 = new DefaultTableModel(datas2, titles2);
- DefaultTableModel myModel3 = new DefaultTableModel(datas3, titles3);
- JTable table = new JTable(myModel);table.setPreferredScrollableViewportSize(new Dimension(700, 100));// 表格的显示尺寸// 表格对象table的数据来源是myModel对象
- JTable table1 = new JTable(myModel1);table1.setPreferredScrollableViewportSize(new Dimension(700, 100));
- JTable table2 = new JTable(myModel2);table2.setPreferredScrollableViewportSize(new Dimension(700, 100));
- JTable table3 = new JTable(myModel3);table3.setPreferredScrollableViewportSize(new Dimension(700, 100));
- // 产生一个带滚动条的面板
- JScrollPane scrollPane = new JScrollPane(table);scrollPane.setBounds(170, 110, 700, 100);
- JScrollPane scrollPane1 = new JScrollPane(table1);scrollPane1.setBounds(170, 220, 700, 100);
- JScrollPane scrollPane2 = new JScrollPane(table2);scrollPane2.setBounds(170, 330, 700, 100);
- JScrollPane scrollPane3 = new JScrollPane(table3);scrollPane3.setBounds(170, 0, 700, 100);
- // 将带滚动条的面板添加入B窗口中
- B.setLayout(null);//自由布局
- B.add(scrollPane);B.add(scrollPane1);B.add(scrollPane2);
- B.add(scrollPane3);B.add(bgimg1);
-
- component1.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- new Add();
- }
- });
- component2.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- new Update();
- }
- });
- component3.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- new Delete();
- }
- });
- component4.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- new Glogin();
- closeThis();
- }
- });
- component7.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- //创建数据库连接对象
- Statement st=conn.createStatement();
- //创建SQL语句执行对象
- while(myModel.getRowCount()>0) {
- myModel.removeRow(myModel.getRowCount()-1);
- }
- while(myModel1.getRowCount()>0) {
- myModel1.removeRow(myModel1.getRowCount()-1);
- }
- while(myModel2.getRowCount()>0) {
- myModel2.removeRow(myModel2.getRowCount()-1);
- }
- while(myModel3.getRowCount()>0) {
- myModel3.removeRow(myModel3.getRowCount()-1);
- }//清空表上的东西
- String ID=component6.getText().trim();
- String s="(Select * from dbo.Student,dbo.PY where Student.Sno='"+ID+"' And Student.Sno=PY.Sno)";
- ResultSet r=st.executeQuery(s);
- while(r.next()) {
- Vector<String> ve = new Vector<String>();
- ve.addElement(r.getString(1));ve.addElement(r.getString(2));
- ve.addElement(r.getString(3));ve.addElement(r.getString(4));
- ve.addElement(r.getString(5));
- myModel.addRow(ve);
- Vector<String> ve1 = new Vector<String>();
- ve1.addElement(r.getString(6));ve1.addElement(r.getString(7));
- ve1.addElement(r.getString(8));ve1.addElement(r.getString(9));
- myModel3.addRow(ve1);
- }
- String s1="(Select * from dbo.SC,dbo.Course where Sno='"+ID+"' And SC.Cno=Course.Cno)";
- ResultSet r1=st.executeQuery(s1);
- while(r1.next()) {
- Vector<String> ve1 = new Vector<String>();
- ve1.addElement(r1.getString(1));ve1.addElement(r1.getString(2));
- ve1.addElement(r1.getString(3));ve1.addElement(r1.getString(4));
- myModel2.addRow(ve1);
- Vector<String> ve2 = new Vector<String>();
- ve2.addElement(r1.getString(5));ve2.addElement(r1.getString(6));
- ve2.addElement(r1.getString(8));
- myModel1.addRow(ve2);
- }
- conn.close();
- }catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- component8.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- //创建数据库连接对象
- Statement st=conn.createStatement();
- //创建SQL语句执行对象
- while(myModel.getRowCount()>0) {
- myModel.removeRow(myModel.getRowCount()-1);
- }
- while(myModel1.getRowCount()>0) {
- myModel1.removeRow(myModel1.getRowCount()-1);
- }
- while(myModel2.getRowCount()>0) {
- myModel2.removeRow(myModel2.getRowCount()-1);
- }
- while(myModel3.getRowCount()>0) {
- myModel3.removeRow(myModel3.getRowCount()-1);
- }
- String strSQL="(Select * from dbo.Student)";
- ResultSet rs=st.executeQuery(strSQL);
- while(rs.next()) {
- Vector<String> v = new Vector<String>();
- v.addElement(rs.getString(1));v.addElement(rs.getString(2));
- v.addElement(rs.getString(3));v.addElement(rs.getString(4));
- v.addElement(rs.getString(5));
- myModel.addRow(v);
- }
- String strSQL1="(Select * from dbo.Course)";
- ResultSet rs1=st.executeQuery(strSQL1);
- while(rs1.next()) {
- Vector<String> v1 = new Vector<String>();
- v1.addElement(rs1.getString(1));v1.addElement(rs1.getString(2));
- v1.addElement(rs1.getString(4));
- myModel1.addRow(v1);
- }
- String strSQL2="(Select * from dbo.SC)";
- ResultSet rs2=st.executeQuery(strSQL2);
- while(rs2.next()) {
- Vector<String> v2 = new Vector<String>();
- v2.addElement(rs2.getString(1));v2.addElement(rs2.getString(2));
- v2.addElement(rs2.getString(3));v2.addElement(rs2.getString(4));
- myModel2.addRow(v2);
- }
- String strSQL3="(Select * from dbo.PY)";
- ResultSet rs3=st.executeQuery(strSQL3);
- while(rs3.next()) {
- Vector<String> v3 = new Vector<String>();
- v3.addElement(rs3.getString(1));v3.addElement(rs3.getString(2));
- v3.addElement(rs3.getString(3));v3.addElement(rs3.getString(4));
- myModel3.addRow(v3);
- }
- conn.close();
- }catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- }
- public void closeThis() {
- this.dispose();
- }
- }
对于增删改这三部分的内容的话,其实还是比较简单的,所以,再次就不多说了,自己可以好好看看你,很容易理解!!
- package 高校学生管理库;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- import javax.swing.*;
-
- public class Add extends JFrame{
- private static final long serialVersionUID = 1L;
- final int WIDTH = 550;
- final int HEIGHT = 315;
- public Add() {
- super("添加信息");
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- this.setSize(1020,700);
- this.setLocationRelativeTo(null);
- setVisible(true);
-
- //初始化字体
- font fonts = new font();
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图四.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- bgimg.setOpaque(false);//背景照片透明
- //给盘子1创建对象
- javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
- jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
- jpanel_1.setLayout(null);//设置布局为空
- //给盘子2创建对象
- javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
- jpanel_2.setBounds(0,30,WIDTH,100);
- FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
- jpanel_2.setLayout(flowLayout);//设置布局为居中
- jpanel_2.setOpaque(false);//设置盘子为透明
- //给盘子3创建对象
- javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
- jpanel_3.setBounds(0,50,WIDTH,350);
- jpanel_3.setLayout(null);//设置布局为空
- jpanel_3.setOpaque(false);//设置盘子为透明
-
- JLabel title = new JLabel(" 添 加 界 面 ");
- title.setFont(font.title);
- title.setHorizontalAlignment(SwingConstants.CENTER);//居中
-
- JLabel j=new JLabel("学号:");JTextField c=new JTextField(15);//学号
- j.setBounds(40, 30, 50, 20); c.setBounds(120, 30, 100, 25);
- JLabel j1=new JLabel("姓名:");JTextField c1=new JTextField(15);//姓名
- j1.setBounds(40, 70, 50, 20);c1.setBounds(120, 70, 100, 25);
- JLabel j2=new JLabel("性别:");JTextField c2=new JTextField(15);//性别
- j2.setBounds(40, 110, 50, 30);c2.setBounds(120, 110, 100, 25);
- JLabel j3=new JLabel("年龄:");JTextField c3=new JTextField(15);//年龄
- j3.setBounds(40, 150, 50, 30);c3.setBounds(120, 150, 100, 25);
- JLabel j4=new JLabel("专业:");JTextField c4=new JTextField(15);//专业
- j4.setBounds(40, 190, 50, 30);c4.setBounds(120, 190, 100, 25);
- JLabel j5=new JLabel("课程:");JTextField c5=new JTextField(15);//课程
- j5.setBounds(40, 230, 50, 30);c5.setBounds(120, 230, 100, 25);
- JLabel j6=new JLabel("成绩:");JTextField c6=new JTextField(15);//成绩
- j6.setBounds(40, 270, 50, 30);c6.setBounds(120, 270, 100, 25);
- JLabel j7=new JLabel("等级:");JTextField c7=new JTextField(15);//等级
- j7.setBounds(40, 310, 50, 30); c7.setBounds(120, 310, 100, 25);
- JLabel j8=new JLabel("(温馨提醒:添加学生信息学号必填哦~,右边一列为课程信息)");j8.setBounds(10, 600, 500, 30);
- JLabel j11=new JLabel("课程号:");JTextField c11=new JTextField(15);//课程号
- j11.setBounds(300, 120, 70, 30); c11.setBounds(380, 120, 100, 25);
- JLabel j12=new JLabel("课程名:");JTextField c12=new JTextField(15);//课程名
- j12.setBounds(300, 170, 70, 30); c12.setBounds(380, 170, 100, 25);
- JLabel j13=new JLabel("学分:");JTextField c13=new JTextField(15);//学分
- j13.setBounds(300, 230, 70, 30); c13.setBounds(380, 230, 100, 25);
-
- JButton a=new JButton("确定");
- a.setContentAreaFilled(false);
- a.setBounds(100, 400, 100, 30);
- JButton b=new JButton("重置");
- b.setContentAreaFilled(false);
- b.setBounds(300, 400, 100, 30);
-
- jpanel_3.add(j); jpanel_3.add(j1); jpanel_3.add(j2);
- jpanel_3.add(j3); jpanel_3.add(j4); jpanel_3.add(j5);
- jpanel_3.add(j6); jpanel_3.add(j7); jpanel_3.add(j11);
- jpanel_3.add(j12);jpanel_3.add(j13);jpanel_1.add(j8);
- jpanel_3.add(c);jpanel_3.add(c1); jpanel_3.add(c2);
- jpanel_3.add(c3);jpanel_3.add(c4); jpanel_3.add(c5);
- jpanel_3.add(c6);jpanel_3.add(c7);jpanel_3.add(c11);
- jpanel_3.add(c12);jpanel_3.add(c13);jpanel_1.add(a);
- jpanel_1.add(b);this.add(jpanel_3);jpanel_2.add(title);
- this.add(jpanel_2);jpanel_1.add(bgimg);this.add(jpanel_1);
-
- a.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- Statement st=conn.createStatement();
-
- String a=c.getText().trim(); String a1=c1.getText().trim();
- String a2=c3.getText().trim(); String a3=c5.getText().trim();
- String a4=c6.getText().trim(); String a11=c11.getText().trim();
- String a12=c12.getText().trim();String a13=c13.getText().trim();
- String a5=c2.getText().trim(); String a6=c4.getText().trim();
- String a7=c7.getText().trim();
-
- String s="(Select * from dbo.Student where Sno='"+a+"')";
- ResultSet r=st.executeQuery(s);
- if(r.next()) {
- JOptionPane.showMessageDialog(null, "该同学已存在哦~","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- else {
- if(a.equals("")) {
- String s3="insert into dbo.Course(Cno,Cname,Ccredit) values('"+a11+"','"+a12+"','"+a13+"')";
- int r3=st.executeUpdate(s3);
- if(r3==1) {
- JOptionPane.showMessageDialog(null, "添加成功","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- }
- else {
- String s1="insert into dbo.Student(Sno,Sname,Ssex,Sage,Sdept) values('"+a+"','"+a1+"','"+a5+"','"+a2+"','"+a6+"')";
- int r1=st.executeUpdate(s1);
- String s2="insert into dbo.SC(Sno,Cno,Grade,LEVEL) values('"+a+"','"+a3+"','"+a4+"','"+a7+"')";
- int r2=st.executeUpdate(s2);
- if(r1==1&&r2==1) {
- JOptionPane.showMessageDialog(null, "添加成功","提示消息",JOptionPane.WARNING_MESSAGE);
- }
- }
- }
- conn.close();
- }catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- b.addActionListener(new ActionListener(){//重置清零
- public void actionPerformed(ActionEvent e) {
- c.setText("");c1.setText("");c2.setText("");
- c3.setText("");c4.setText("");c5.setText("");
- c6.setText("");c11.setText("");c12.setText("");
- c13.setText("");
- }
- });
- }
- }
- package 高校学生管理库;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- import javax.swing.*;
-
- public class Delete extends JFrame{
- private static final long serialVersionUID = 1L;
- final int WIDTH = 500;
- final int HEIGHT = 315;
- public Delete() {
- super("删除信息");
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- this.setSize(1020,700);
- this.setLocationRelativeTo(null);
- setVisible(true);
-
- //初始化字体
- font fonts = new font();
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图五.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- bgimg.setOpaque(false);//将面板设置为透明
- //给盘子1创建对象
- javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
- jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
- jpanel_1.setLayout(null);//设置布局为空
- jpanel_1.setOpaque(false);
- //给盘子2创建对象
- javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
- jpanel_2.setBounds(0,30,WIDTH,100);
- FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
- jpanel_2.setLayout(flowLayout);//设置布局为居中
- jpanel_2.setOpaque(false);//设置盘子为透明
- //给盘子3创建对象
- javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
- jpanel_3.setBounds(0,50,WIDTH,350);
- jpanel_3.setLayout(null);//设置布局为空
- jpanel_3.setOpaque(false);//设置盘子为透明
-
- JLabel title = new JLabel(" 删 除 界 面 ");
- title.setFont(font.title);
- title.setHorizontalAlignment(SwingConstants.CENTER);//居中
-
- JCheckBox optionA=new JCheckBox(" 姓名");optionA.setBounds(80, 70, 150, 20);
- JCheckBox optionB=new JCheckBox(" 性别");optionB.setBounds(80, 120, 150, 20);
- JCheckBox optionC=new JCheckBox(" 年龄");optionC.setBounds(80, 170, 150, 20);
- JCheckBox optionD=new JCheckBox(" 专业");optionD.setBounds(80, 220, 150, 20);
- JCheckBox optionE=new JCheckBox(" 成绩");optionE.setBounds(80, 320, 150, 20);
- JCheckBox optionF=new JCheckBox(" 等级");optionF.setBounds(80, 370, 150, 20);
- optionA.setContentAreaFilled(false);optionB.setContentAreaFilled(false);
- optionC.setContentAreaFilled(false);optionD.setContentAreaFilled(false);
- optionE.setContentAreaFilled(false);optionF.setContentAreaFilled(false);
- JLabel j=new JLabel("学号:");j.setBounds(30, 20, 50, 20);
- JLabel j1=new JLabel("(温馨提醒:谨慎删除哦~)");j1.setBounds(10, 600, 450, 15);
- JLabel j11=new JLabel("课程号:");j11.setBounds(300, 150, 70, 30);
- JLabel j12=new JLabel("课程名:");j12.setBounds(300, 250, 70, 30);
- JLabel cc=new JLabel("课程号:");cc.setBounds(20, 270, 70, 20);
- JTextField c=new JTextField(12);c.setBounds(90, 270, 150, 25);
- JTextField c1=new JTextField(12);c1.setBounds(90, 20, 150, 25);
- JTextField c11=new JTextField(10);c11.setBounds(380, 150, 100, 25);
- JTextField c12=new JTextField(10);c12.setBounds(380, 250, 100, 25);
-
- JButton a=new JButton("确定");a.setBounds(100, 400, 100, 30);
- a.setContentAreaFilled(false);
- JButton b=new JButton("重置");b.setBounds(300, 400, 100, 30);
- b.setContentAreaFilled(false);
-
- jpanel_1.add(a);jpanel_1.add(b);jpanel_3.add(j11);
- jpanel_3.add(c);jpanel_3.add(cc); jpanel_3.add(c12); jpanel_1.add(j1);
- jpanel_3.add(j);jpanel_3.add(c1);jpanel_3.add(c11); jpanel_3.add(j12);
- jpanel_3.add(optionA);jpanel_3.add(optionB);jpanel_3.add(optionC);
- jpanel_3.add(optionD);jpanel_3.add(optionE);jpanel_3.add(optionF);
- this.add(jpanel_3);jpanel_2.add(title);this.add(jpanel_2);
- jpanel_1.add(bgimg);this.add(jpanel_1);
-
- a.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- //创建数据库连接对象
- Statement st=conn.createStatement();
- //创建SQL语句执行对象
- String a=c1.getText().trim();
- String a1=c.getText().trim();
- String s="(Select * from dbo.Student where Sno='"+a+"')";
- ResultSet r=st.executeQuery(s);
- if(r.next()) {
- if(optionA.isSelected()) {
- String strSQL="update dbo.Student set Sname="+null+" where Sno='"+a+"'";
- st.executeUpdate(strSQL);
- }
- if(optionB.isSelected()) {
- String strSQL="update dbo.Student set Ssex="+null+" where Sno='"+a+"'";
- st.executeUpdate(strSQL);
- }
- if(optionC.isSelected()) {
- String strSQL="update dbo.Student set Sage="+null+" where Sno='"+a+"'";
- st.executeUpdate(strSQL);
- }
- if(optionD.isSelected()) {
- String strSQL="update dbo.Student set Sdept="+null+" where Sno='"+a+"'";
- st.executeUpdate(strSQL);
- }
- if(optionE.isSelected()) {
- String strSQL="update dbo.SC set Grade="+null+" where Sno='"+a+"' And Cno='"+a1+"'";
- st.executeUpdate(strSQL);
- }
- if(optionF.isSelected()) {
- String strSQL="update dbo.SC set LEVEL="+null+" where Sno='"+a+"' And Cno='"+a1+"'";
- st.executeUpdate(strSQL);
- }
- JOptionPane.showMessageDialog(null,"删除成功哦~");
- }
- else {
- String cc=c11.getText().trim();
- String strSQL="delete from dbo.Course where Cno='"+cc+"' ";
- int rr=st.executeUpdate(strSQL);
- if(rr==1) {
- JOptionPane.showMessageDialog(null,"删除成功哦~");
- }
- else {
- JOptionPane.showMessageDialog(null,"课程不存在哦~");
- }
- }
- conn.close();
- }catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- b.addActionListener(new ActionListener(){//重置清零
- public void actionPerformed(ActionEvent e) {
- c1.setText("");c11.setText("");c12.setText("");
- optionA.setSelected(false);optionB.setSelected(false);optionC.setSelected(false);
- optionD.setSelected(false);optionE.setSelected(false);optionF.setSelected(false);
- }
- });
- }
- }
- package 高校学生管理库;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- import javax.swing.*;
-
- public class Update extends JFrame{
- private static final long serialVersionUID = 1L;
- final int WIDTH = 550;
- final int HEIGHT = 315;
- public Update() {
- super("修改信息");
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- this.setSize(1020,700);
- this.setLocationRelativeTo(null);
- setVisible(true);
-
- //初始化字体
- font fonts = new font();
- //设置背景图片
- ImageIcon img = new ImageIcon("C:\\Users\\god36c\\OneDrive\\桌面\\图六.jpg");
- JLabel bgimg = new JLabel(img);
- bgimg.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
- bgimg.setOpaque(false);//将面板设置为透明
- //给盘子1创建对象
- javax.swing.JPanel jpanel_1 = new javax.swing.JPanel();
- jpanel_1.setBounds(0,0,WIDTH,HEIGHT);
- jpanel_1.setLayout(null);//设置布局为空
- jpanel_1.setOpaque(false);//设置盘子为透明
- //给盘子2创建对象
- javax.swing.JPanel jpanel_2 = new javax.swing.JPanel();
- jpanel_2.setBounds(0,30,WIDTH,100);
- FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
- jpanel_2.setLayout(flowLayout);//设置布局为居中
- jpanel_2.setOpaque(false);//设置盘子为透明
- //给盘子3创建对象
- javax.swing.JPanel jpanel_3 = new javax.swing.JPanel();
- jpanel_3.setBounds(0,50,WIDTH,350);
- jpanel_3.setLayout(null);//设置布局为空
- jpanel_3.setOpaque(false);//设置盘子为透明
-
- JLabel title = new JLabel(" 修 改 界 面 ");
- title.setFont(font.title);
- title.setHorizontalAlignment(SwingConstants.CENTER);//居中
-
- JLabel j=new JLabel("学号:");j.setBounds(20, 30, 50, 20);
- JTextField c=new JTextField(15);c.setBounds(80, 30, 120, 25);//学号
- JLabel j1=new JLabel("姓名:");j1.setBounds(20, 70, 50, 20);
- JTextField c1=new JTextField(15);c1.setBounds(80, 70, 100, 25);//姓名
- JLabel j2=new JLabel("性别:");j2.setBounds(20, 110, 50, 30);
- JTextField c2=new JTextField(15);c2.setBounds(80, 110, 100, 25);//性别
- JLabel j3=new JLabel("年龄:");j3.setBounds(20, 150, 50, 30);
- JTextField c3=new JTextField(15);c3.setBounds(80, 150, 100, 25);//年龄
- JLabel j4=new JLabel("专业:");j4.setBounds(20, 190, 50, 30);
- JTextField c4=new JTextField(15);c4.setBounds(80, 190, 100, 25);//专业
- JLabel j5=new JLabel("课程:");j5.setBounds(20, 230, 50, 30);
- JTextField c5=new JTextField(20);c5.setBounds(80, 230, 100, 25);//课程
- JLabel j6=new JLabel("成绩:");j6.setBounds(20, 270, 50, 30);
- JTextField c6=new JTextField(20);c6.setBounds(80, 270, 100, 25);//成绩
- JLabel j7=new JLabel("等级:");j7.setBounds(20, 310, 50, 30);
- JTextField c7=new JTextField(15);c7.setBounds(80, 310, 100, 25);//等级
- JLabel j00=new JLabel("旧课程号:");j00.setBounds(290, 100, 90, 30);
- JTextField c00=new JTextField(15);c00.setBounds(380, 100, 100, 25);//旧课程号
- JLabel j11=new JLabel("新课程号:");j11.setBounds(290, 150, 90, 30);
- JTextField c11=new JTextField(15);c11.setBounds(380, 150, 100, 25);//新课程号
- JLabel j12=new JLabel("新课程名:");j12.setBounds(290, 200, 90, 30);
- JTextField c12=new JTextField(15);c12.setBounds(380, 200, 100, 25);//新课程名
- JLabel j13=new JLabel("新学分:");j13.setBounds(300, 250, 90, 30);
- JTextField c13=new JTextField(15);c13.setBounds(380, 250, 100, 25);//新学分
- JLabel j8=new JLabel("(温馨提醒:更新学生信息学号必填哦~右边一列为课程信息)");j8.setBounds(10, 600, 500, 15);
-
- JButton aa=new JButton("确定");aa.setBounds(100, 400, 100, 30);
- aa.setContentAreaFilled(false);
- JButton bb=new JButton("重置");bb.setBounds(300, 400, 100, 30);
- bb.setContentAreaFilled(false);
-
- jpanel_3.add(j); jpanel_3.add(j1);jpanel_3.add(j2);
- jpanel_3.add(j3);jpanel_3.add(j4);jpanel_3.add(j5);
- jpanel_3.add(j6);jpanel_3.add(j7);jpanel_3.add(j11);
- jpanel_3.add(j12);jpanel_3.add(j13);jpanel_3.add(c11);
- jpanel_3.add(c12);jpanel_3.add(c13);jpanel_3.add(c);
- jpanel_3.add(c1); jpanel_3.add(c2); jpanel_3.add(c3);
- jpanel_3.add(c4); jpanel_3.add(c5); jpanel_3.add(c6);
- jpanel_3.add(c7); jpanel_1.add(aa); jpanel_1.add(bb);
- jpanel_1.add(j8);jpanel_3.add(c00);jpanel_3.add(j00);
- this.add(jpanel_3);jpanel_2.add(title);this.add(jpanel_2);
- jpanel_1.add(bgimg);this.add(jpanel_1);
-
- aa.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- //加载对应的jdbc驱动
- String url="jdbc:sqlserver://localhost:1433;DatabaseName=高校学生管理库";
- //配置连接字符串
- String user="sa";//sa超级管理员
- String password="你的密码";//密码
- Connection conn=DriverManager.getConnection(url,user,password);
- Statement st=conn.createStatement();
- //修改过布局所以顺序有点乱=。=
- String a=c.getText().trim();String a1=c1.getText().trim();
- String a2=c3.getText().trim();String a3=c5.getText().trim();
- String a4=c6.getText().trim();String a11=c11.getText().trim();
- String a12=c12.getText().trim();String a13=c13.getText().trim();
- String a5=c2.getText().trim();String a6=c4.getText().trim();String a7=c7.getText().trim();
- String a00=c00.getText().trim();
-
- String s="(Select * from dbo.Student where Sno='"+a+"')";
- ResultSet r=st.executeQuery(s);
- if(r.next()) {
- if (!a1.isEmpty()) {
- String strSQL="update dbo.Student set Sname='"+a1+"' where Sno='"+a+"'";
- st.executeUpdate(strSQL);
- }
- if (!a2.isEmpty()) {
- String strSQL="update dbo.Student set Sage='"+a2+"' where Sno='"+a+"'";
- st.executeUpdate(strSQL);
- }
- if (!a5.isEmpty()) {
- String strSQL="update dbo.Student set Ssex='"+a5+"' where Sno='"+a+"'";
- st.executeUpdate(strSQL);
- }
- if (!a6.isEmpty()) {
- String strSQL="update dbo.Student set Sdept='"+a6+"' where Sno='"+a+"'";
- st.executeUpdate(strSQL);
- }
- if (!a3.isEmpty()) {
- String strSQL="update dbo.SC set Grade='"+a4+"' where Sno='"+a+"' And Cno='"+a3+"'" ;
- String strSQL1="update dbo.SC set LEVEL='"+a7+"' where Sno='"+a+"' And Cno='"+a3+"'" ;
- st.executeUpdate(strSQL);
- st.executeUpdate(strSQL1);
- }
- JOptionPane.showMessageDialog(null,"修改成功哦~");
- }
- else {
- String strSQL="update dbo.Course set Cno='"+a11+"',Cname='"+a12+"',Ccredit='"+a13+"' where Cno='"+a00+"'";
- int rs1=st.executeUpdate(strSQL);
- if(rs1==1) {
- JOptionPane.showMessageDialog(null,"修改成功哦~");
- }
- }
- conn.close();
- }catch (ClassNotFoundException ex) {
- System.out.println("没有找到对应的数据库驱动类");
- }
- catch (SQLException ex) {
- System.out.println("数据库连接或者是数据库操作失败");
- }
- }
- });
- //重置清零
- bb.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- c.setText("");c1.setText("");c2.setText("");
- c3.setText("");c4.setText("");c5.setText("");
- c6.setText("");c11.setText("");c12.setText("");
- c13.setText("");
- }
- });
- }
- }
另外,对于这增删改的界面我单独设置了字体font类。
- package 高校学生管理库;
-
- import java.awt.Font;
-
- public class font {
- public static Font title;
- public static Font account;
- public font() {
- title= new Font("宋体",Font.BOLD,30);
- account = new Font("宋体",Font.BOLD,16);
- }
- }
还有用到的SQL语言:另外,管理员是需要自己设置的哦,你也可以去设置学生细腻
- --照片存放表
- create table image
- ( Sno char(10),
- ima varchar(50),
- );
-
- --学生表
- create table Student
- ( Sno char(9) primary key, /*列级完整性约束条件,Sno是主码 */
- Sname char(20) unique, /*Sname取唯一值 */
- Ssex char(2),
- Sage smallint,
- Sdept char(20)
- );
-
- --Course表
- create table Course
- ( Cno char(4) primary key,/* 列级完整性约束条件,Cno是主码*/
- Cname char(40) NOT NULL, /* 列级完整性约束条件,Cname不能取空值 */
- Cpno char(4),/* Cpno的含义是先修课 */
- Ccredit int,
- foreign key(Cpno) references Course(Cno) /*表级完整性约束条件,Cpno是外码,被参照表是Course,被参照列是Cno */
- );
-
- --信息表
- create table SC
- ( Sno char(9),
- Cno char(4),
- Grade int,
- LEVEL char(4),
- primary key(Sno,Cno),/*主码由两个属性构成,必须作为表级完整性进行定义 */
- foreign key(Sno) references Student(Sno), /*表级完整性约束条件,Sno是外码,被参照表是Student */
- foreign key(Cno) references Course(Cno)/*表级完整性约束条件,Cno是外码,被参照表是Course */
- );
-
- --管理员表
- create table GLY(
- ID1 varchar(30),
- PAWD1 varchar(50)--密码
- );
-
- --学生注册表
- create table PY(
- ID varchar(30),
- PAWD varchar(50),
- Sno char(9),
- phone char(20)
- );
-
- --学生信息
-
- --管理员
-
-
- --插入Course数据
- insert into Course values('1','数据库',null,4)
- insert into Course values('2','高等数学A',null,2)
- insert into Course values('3','信息系统',null,4)
- insert into Course values('4','操作系统',null,3)
- insert into Course values('5','数据结构','5',4)
- insert into Course values('6','计算机网络','5',4)
- insert into Course values('7','离散数学','5',2)
- insert into Course values('8','大学英语','5',4)
- insert into Course values('9','计算机组成原理','5',3)
- insert into Course values('10','算法设计与分析','5',4)
- insert into Course values('11','大学物理A',null,4)
- insert into Course values('12','高等数学B',null,2)
- insert into Course values('13','大学物理B',null,4)
- insert into Course values('14','计算机英语',null,3)
- insert into Course values('15','算法导论','5',4)
- insert into Course values('16','数据库原理与分析','5',4)
- insert into Course values('17','数据挖掘','5',2)
- insert into Course values('18','移动应用开发','5',4)
- insert into Course values('19','Javaweb开发应用','5',3)
-
- --课程等级
图三:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。