码迷,mamicode.com
首页 > 数据库 > 详细

nwafu - java实习 JDBC练习 - 学生信息系统界面

时间:2017-12-22 14:56:35      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:rri   函数   als   图形   查询   student   密码   rac   mes   

学生信息系统界面的实现 - JDBC

writer:pprp

登录界面的实现:

分为两个部分:

1、LoginFrame.java :

用windowbuilder进行快速搭建界面,构建好登录的界面,并用LogConnection类构建对象,进行处理,增加监听器。

2、LogConnection.java:

用基本的JDBC步骤进行处理,其中多加了一个判断用户名和密码匹配问题。

注意的问题:

1、只有在ResultSet对象使用完毕以后才能关掉Connection对象,否则会影响数据的传输,最后将连接关闭。

2、另外要注意在使用ResultSet进行结果查询的时候,需要加入一个判断,if(!rs.next())return ;否则会报错。

3、注意close函数应该在LogConnection中写,在LoginFrame的监听器中调用关闭。

LoginFrame.java

package work2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import work3.StudentFrame;

@SuppressWarnings("serial")
public class LoginFrame extends JFrame {

    private JPanel contentPane;
    private JTextField userFiled;
    private JPasswordField pwd;
    private LogConnection lc = null;

    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        LoginFrame frame = new LoginFrame();
        frame.setVisible(true);
    }

    public LoginFrame() {
        setTitle("学生信息管理系统");
        lc = new LogConnection();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
        lblNewLabel.setBounds(63, 68, 54, 15);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("\u5BC6  \u7801\uFF1A");
        lblNewLabel_1.setBounds(63, 128, 54, 15);
        contentPane.add(lblNewLabel_1);

        userFiled = new JTextField();
        userFiled.setBounds(127, 65, 224, 23);
        contentPane.add(userFiled);
//      userFiled.setColumns(10);

        pwd = new JPasswordField();
        pwd.setBounds(127, 124, 224, 23);
        contentPane.add(pwd);

        JButton loginBtn = new JButton("\u786E\u5B9A");
        loginBtn.setBounds(63, 204, 93, 23);
        contentPane.add(loginBtn);

        JButton cancelBtn = new JButton("\u53D6\u6D88");
        cancelBtn.setBounds(268, 204, 93, 23);
        contentPane.add(cancelBtn);

        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                String name = userFiled.getText().trim();
                String pwdText = String.valueOf(pwd.getPassword()).trim();
                if (lc.Judge(name, pwdText)) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "欢迎您,"
                            + name);
                    @SuppressWarnings("unused")     
                    StudentFrame tmp = new StudentFrame();
                    tmp.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "用 户名或密码错误");
                }
            }

        });
        cancelBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.exit(0);
                lc.close();
            }

        });
        JOptionPane.showMessageDialog(LoginFrame.this, "欢迎登陆学生信息管理系统,请输入账号密码");
    }
}

LogConnection.java

package work2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LogConnection {
    Connection con = null;
    Statement sql = null;

    public LogConnection() {
        // 1 drive
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("驱动加载成功");
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败");
            e.printStackTrace();
        }
        // 2 connect

        String url = "jdbc:mysql://localhost/test?useSSL=true";
        String user = "root";
        String password = "root";
        try {
            con = DriverManager.getConnection(url, user, password);
            System.out.println("链接成功");
        } catch (SQLException e) {
            System.out.println("链接失败");
            e.printStackTrace();
        }
        // 3 statement object
        try {
            sql = con.createStatement();
            System.out.println("成功生成statement对象");
        } catch (SQLException e) {
            System.out.println("生成statement对象失败");
            e.printStackTrace();
        }

    }

    public boolean Judge(String name, String passwd) {
        ResultSet rs = null;
        String str = "select * from log where logname = '" + name + "'";
        try {
            rs = sql.executeQuery(str);
            if (!rs.next())
                return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        try {
            String cmpPwd = rs.getString(3);
            if (cmpPwd.equals(passwd))
                return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
    public void close(){
        try {
            con.close();
            System.out.println("关闭成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("关闭失败");
        }
    }
}

查询界面的实现:

要求:

设计学生信息管理系统。

使用Navicat的test数据库,创建Student表,包含学生的学号、姓名、年龄信息。根据以下的功能,编写相应的函数:

① 根据学号,可以查询到学生的姓名和年龄;

② 给定学生的学号、姓名、年龄,在表中追加一行信息;

③ 给定学生的学号,可以从表中删除该学生的信息;
使用图形界面实现任务三的学生信息管理系统设计。

分析:

用到上一个部分的登录界面,在登录界面的ActionListener中处理,如果成功登录,则打开查询、更新、删除界面进行操作。

Student.java: 其中是关于JDBC编程内容,为三个需求设计了三个函数,分别实现三个功能要求。

StudentFrame.java: 界面的设计,为四个按钮注册监听器,综合实现三种功能。

知识点:

1、用到的SQL语句比较多,要注意使用的时候需要在适当的时候加双引号,例如:
"insert into Student(sname,age)
values(‘"+name+"‘,"+age+")"
其中name和age都是变量。

2、注意数据库中编号是从1开始的,不是从0开始。

3、executeUpdate语句:是执行增删改的操作。
executeQuery语句:是执行查询操作的。

LoginFrame.java

package work2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import work3.StudentFrame;

@SuppressWarnings("serial")
public class LoginFrame extends JFrame {

    private JPanel contentPane;
    private JTextField userFiled;
    private JPasswordField pwd;
    private LogConnection lc = null;

    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        LoginFrame frame = new LoginFrame();
        frame.setVisible(true);
    }

    public LoginFrame() {
        setTitle("学生信息管理系统");
        lc = new LogConnection();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
        lblNewLabel.setBounds(63, 68, 54, 15);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("\u5BC6  \u7801\uFF1A");
        lblNewLabel_1.setBounds(63, 128, 54, 15);
        contentPane.add(lblNewLabel_1);

        userFiled = new JTextField();
        userFiled.setBounds(127, 65, 224, 23);
        contentPane.add(userFiled);
//      userFiled.setColumns(10);

        pwd = new JPasswordField();
        pwd.setBounds(127, 124, 224, 23);
        contentPane.add(pwd);

        JButton loginBtn = new JButton("\u786E\u5B9A");
        loginBtn.setBounds(63, 204, 93, 23);
        contentPane.add(loginBtn);

        JButton cancelBtn = new JButton("\u53D6\u6D88");
        cancelBtn.setBounds(268, 204, 93, 23);
        contentPane.add(cancelBtn);

        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                String name = userFiled.getText().trim();
                String pwdText = String.valueOf(pwd.getPassword()).trim();
                if (lc.Judge(name, pwdText)) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "欢迎您,"
                            + name);
                    @SuppressWarnings("unused")     
                    StudentFrame tmp = new StudentFrame();
                    tmp.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "用 户名或密码错误");
                }
            }

        });
        cancelBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.exit(0);
                lc.close();
            }

        });
        JOptionPane.showMessageDialog(LoginFrame.this, "欢迎登陆学生信息管理系统,请输入账号密码");
    }
}


LogConnection.java

package work2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LogConnection {
    Connection con = null;
    Statement sql = null;

    public LogConnection() {
        // 1 drive
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("驱动加载成功");
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败");
            e.printStackTrace();
        }
        // 2 connect

        String url = "jdbc:mysql://localhost/test?use分析:SSL=true";
        String user = "root";
        String password = "root";
        try {
            con = DriverManager.getConnection(url, user, password);
            System.out.println("链接成功");
        } catch (SQLException e) {
            System.out.println("链接失败");
            e.printStackTrace();
        }
        // 3 statement object
        try {
            sql = con.createStatement();
            System.out.println("成功生成statement对象");
        } catch (SQLException e) {
            System.out.println("生成statement对象失败");
            e.printStackTrace();
        }

    }

    public boolean Judge(String name, String passwd) {
        ResultSet rs = null;
        String str = "select * from log where logname = '" + name + "'";
        try {
            rs = sql.executeQuery(str);
            if (!rs.next())
                return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        try {
            String cmpPwd = rs.getString(3);
            if (cmpPwd.equals(passwd))
                return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
    public void close(){
        try {
            con.close();
            System.out.println("关闭成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("关闭失败");
        }
    }
}

如果感觉有帮助,请点个赞

nwafu - java实习 JDBC练习 - 学生信息系统界面

标签:rri   函数   als   图形   查询   student   密码   rac   mes   

原文地址:http://www.cnblogs.com/pprp/p/8085510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!