1. 程式人生 > >nwafu - java實習 JDBC練習 - 學生信息系統界面

nwafu - java實習 JDBC練習 - 學生信息系統界面

creates 打開 resultset 數據庫 jbutton lean rman erp nav

學生信息系統界面的實現 - 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練習 - 學生信息系統界面