(尊重勞動成果,轉載請註明出處:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客)
java
圖形用戶界面(Graphics User Interface,GUI)是用戶與程序交互的窗口,比命令行的界面更加直觀而且更好操做。sql
這是本人在學習Java圖形界面開發階段一步一步實現的超級簡易的學生管理系統。雖說不入大神法眼,但這確實是費了本身很多心血。對於我這樣的菜鳥來講,考慮不周到,一不當心就Exception,而後就是本身調呀調。在此分享出來但願對和我同樣的菜鳥有幫助。數據庫
該程序使用的數據庫爲SQL server,若是不太懂怎麼使用java操做SQL server數據庫的能夠看看個人這篇博客,但願能夠幫到你們。小程序
Java程序操做數據庫SQLserver詳解 設計模式
程序完整代碼下載地址見:jsp
http://download.csdn.net/detail/qq_25827845/9766699ide
步驟一:靜態登陸界面的實現。函數
代碼以下:佈局
-
-
-
-
-
- import java.awt.*;
- import javax.swing.*;
- public class Login extends JFrame{
-
- JPanel jp1,jp2,jp3;
- JLabel jlb1,jlb2;
- JButton jb1,jb2;
- JTextField jtf;
- JPasswordField jpf;
- public static void main(String[] args) {
- Login win=new Login();
- }
-
-
- public Login(){
-
- jp1=new JPanel();
- jp2=new JPanel();
- jp3=new JPanel();
-
- jlb1=new JLabel("用戶名");
- jlb2=new JLabel("密 碼");
-
- jb1=new JButton("登陸");
- jb2=new JButton("重置");
-
- jtf=new JTextField(10);
-
- jpf=new JPasswordField(10);
-
-
- this.setLayout(new GridLayout(3, 1));
-
-
- jp1.add(jlb1);
- jp1.add(jtf);
-
- jp2.add(jlb2);
- jp2.add(jpf);
-
- jp3.add(jb1);
- jp3.add(jb2);
-
-
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
-
-
- this.setTitle("用戶登陸");
- this.setSize(300, 150);
- this.setLocationRelativeTo(null);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
-
-
- this.setResizable(false);
- }
- }
運行界面如圖所示:
本靜態界面的實現,使用了網格佈局,3行1列,在每一行中放入了一個JPanel面板,每一個面板上又分別放入所需的組件,總體構成了一個靜態的登陸界面。學習
步驟二:添加監聽,而且進行驗證用戶名和密碼。
代碼以下:
-
-
-
-
-
-
-
- package com.package_1;
- import javax.swing.*;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- public class Login extends JFrame implements ActionListener {
-
-
- JButton jb1,jb2=null;
- JRadioButton jrb1,jrb2=null;
- JPanel jp1,jp2,jp3,jp4=null;
- JTextField jtf=null;
- JLabel jlb1,jlb2,jlb3=null;
- JPasswordField jpf=null;
- ButtonGroup bg=null;
-
-
- final String stu_name="6";
- final String stu_pwd="1";
- final String tea_name="5";
- final String tea_pwd="1";
-
- public static void main(String[] args) {
-
- Login ms=new Login();
- }
- public Login()
- {
-
- jb1=new JButton("登陸");
- jb2=new JButton("重置");
-
- jb1.addActionListener(this);
- jb2.addActionListener(this);
-
- jrb1=new JRadioButton("教師");
- jrb2=new JRadioButton("學生");
- bg=new ButtonGroup();
- bg.add(jrb1);
- bg.add(jrb2);
- jrb2.setSelected(true);
-
- jp1=new JPanel();
- jp2=new JPanel();
- jp3=new JPanel();
- jp4=new JPanel();
-
- jlb1=new JLabel("用戶名:");
- jlb2=new JLabel("密 碼:");
- jlb3=new JLabel("權 限:");
-
- jtf=new JTextField(10);
- jpf=new JPasswordField(10);
-
- jp1.add(jlb1);
- jp1.add(jtf);
-
- jp2.add(jlb2);
- jp2.add(jpf);
-
- jp3.add(jlb3);
- jp3.add(jrb1);
- jp3.add(jrb2);
-
- jp4.add(jb1);
- jp4.add(jb2);
-
-
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
- this.add(jp4);
-
- this.setLayout(new GridLayout(4,1));
-
- this.setTitle("學生成績管理系統");
-
- this.setSize(300,200);
-
- this.setLocation(200, 150);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.setVisible(true);
- this.setResizable(true);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
-
- if(e.getActionCommand()=="登陸")
- {
-
- if(jrb1.isSelected())
- {
- tealogin();
- }else if(jrb2.isSelected())
- {
- stulogin();
- }
-
- }else if(e.getActionCommand()=="重置")
- {
- clear();
- }
-
- }
-
-
- public void stulogin()
- {
- if(stu_name.equals(jtf.getText())&&stu_pwd.equals(jpf.getText()))
- {
-
- JOptionPane.showMessageDialog(null,"登陸成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- clear();
- }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名和密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- JOptionPane.showMessageDialog(null,"用戶名或者密碼錯誤!\n請從新輸入","提示消息",JOptionPane.ERROR_MESSAGE);
-
- clear();
- }
- }
-
- public void tealogin()
- {
- if(tea_name.equals(jtf.getText())&&tea_pwd.equals(jpf.getText()))
- {
-
- JOptionPane.showMessageDialog(null,"登陸成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- clear();
- }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名和密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- JOptionPane.showMessageDialog(null,"用戶名或者密碼錯誤!\n請從新輸入","提示消息",JOptionPane.ERROR_MESSAGE);
-
- clear();
- }
- }
-
- public void clear()
- {
- jtf.setText("");
- jpf.setText("");
- }
-
- }
運行界面以下所示:
本程序加入了對各個組件的監聽,首先implements ActionListener接口,而後註冊監聽。在ActionPerformance方法中進行相應的處理事件。
當點擊登陸按鈕時,首先判斷是哪一個權限的用戶在進行登陸,分別調用taelogin()和stulogin()方法來進行驗證。在驗證方法中,對文本框和密碼框的內容進行一個判斷,分別彈出不一樣的提示信息。
步驟三:(1)程序鏈接數據庫來進行用戶名和密碼的驗證!!!
(2)驗證成功後進行界面的切換!!!
代碼以下:
-
-
-
-
-
-
-
- package com.package_2;
- import javax.swing.*;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.*;
-
- public class login extends JFrame implements ActionListener {
-
-
- JButton jb1,jb2,jb3=null;
- JRadioButton jrb1,jrb2=null;
- JPanel jp1,jp2,jp3,jp4=null;
- JTextField jtf=null;
- JLabel jlb1,jlb2,jlb3=null;
- JPasswordField jpf=null;
- ButtonGroup bg=null;
-
-
- static String userword;
- static String pwd;
-
- static Connection ct=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
-
- public static void main(String[] args) {
-
- login ms=new login();
-
-
- try {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- ct=DriverManager.getConnection("jdbc:odbc:ywq");
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
- }
-
- public login()
- {
-
- jb1=new JButton("登陸");
- jb2=new JButton("重置");
- jb3=new JButton("退出");
-
- jb1.addActionListener(this);
- jb2.addActionListener(this);
- jb3.addActionListener(this);
-
-
-
-
-
-
-
-
-
-
-
-
-
- jrb1=new JRadioButton("教師");
- jrb2=new JRadioButton("學生");
- bg=new ButtonGroup();
- bg.add(jrb1);
- bg.add(jrb2);
- jrb2.setSelected(true);
-
- jp1=new JPanel();
- jp2=new JPanel();
- jp3=new JPanel();
- jp4=new JPanel();
-
- jlb1=new JLabel("用戶名:");
- jlb2=new JLabel("密 碼:");
- jlb3=new JLabel("權 限:");
-
- jtf=new JTextField(10);
- jpf=new JPasswordField(10);
-
- jp1.add(jlb1);
- jp1.add(jtf);
-
- jp2.add(jlb2);
- jp2.add(jpf);
-
- jp3.add(jlb3);
- jp3.add(jrb1);
- jp3.add(jrb2);
-
- jp4.add(jb1);
- jp4.add(jb2);
- jp4.add(jb3);
-
-
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
- this.add(jp4);
-
- this.setLayout(new GridLayout(4,1));
-
- this.setTitle("學生成績管理系統");
-
- this.setSize(300,200);
-
- this.setLocation(200, 150);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.setVisible(true);
- this.setResizable(true);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
-
- if(e.getActionCommand()=="退出")
- {
- System.exit(0);
- }else if(e.getActionCommand()=="登陸")
- {
-
- if(jrb1.isSelected())
- {
-
- try {
- ps=ct.prepareStatement("select * from info where 權限=? ");
-
- ps.setString(1, "教師");
-
- rs=ps.executeQuery();
-
- while(rs.next()){
-
- userword=rs.getString(2);
- pwd=rs.getString(3);
- System.out.println("成功獲取到密碼和用戶名from數據庫");
- System.out.println(userword+"\t"+pwd+"\t");
- }
- } catch (SQLException e1) {
-
- e1.printStackTrace();
- }
-
- tealogin();
- }else if(jrb2.isSelected())
- {
-
- try {
- ps=ct.prepareStatement("select * from info where 權限=? ");
-
- ps.setString(1, "學生");
-
- rs=ps.executeQuery();
-
- while(rs.next()){
-
- userword=rs.getString(2);
- pwd=rs.getString(3);
- System.out.println("成功獲取到密碼和用戶名from數據庫");
- System.out.println(userword+"\t"+pwd+"\t");
- }
- } catch (SQLException e1) {
-
- e1.printStackTrace();
- }
-
- stulogin();
- }
-
- }else if(e.getActionCommand()=="重置")
- {
- clear();
- }
-
- }
-
-
-
- public void clear()
- {
- jtf.setText("");
- jpf.setText("");
- }
-
- public void stulogin()
- {
- if(userword.equals(jtf.getText())&&pwd.equals(jpf.getText()))
- {
-
- JOptionPane.showMessageDialog(null,"登陸成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- clear();
-
- dispose();
-
- UI ui=new UI();
- }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名和密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- JOptionPane.showMessageDialog(null,"用戶名或者密碼錯誤!\n請從新輸入","提示消息",JOptionPane.ERROR_MESSAGE);
-
- clear();
- }
- }
-
-
- public void tealogin()
- {
- if(userword.equals(jtf.getText())&&pwd.equals(jpf.getText()))
- {
-
- JOptionPane.showMessageDialog(null,"登陸成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- clear();
-
- dispose();
-
- UI ui=new UI();
- }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名和密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- JOptionPane.showMessageDialog(null,"用戶名或者密碼錯誤!\n請從新輸入","提示消息",JOptionPane.ERROR_MESSAGE);
-
- clear();
- }
- }
-
-
-
- }
首先創建相應的數據庫,如圖所示:
在程序中,首先是在主函數中(1)加載數據庫驅動 。(2)創建鏈接
此處有問題,可參考博客 http://blog.csdn.net/qq_25827845/article/details/50836362
在進行驗證時,經過創建「火箭車」將所需的SQL語句發送到數據庫,而且查詢獲得相應的數據。利用此數據和用戶輸入的用戶名和密碼進行驗證。當驗證成功時,即進行界面的跳轉。
頁面跳轉關鍵部分:
(1)調用dispose()方法關閉當前界面
(2)從新new一個新界面,好比本例中的new UI();其中,UI是另外一個完整的界面。
如此實現了界面的切換。
UI界面的代碼以下:
- package com.package_2;
-
- import java.awt.*;
- import java.awt.event.*;
-
- import javax.swing.*;
-
- public class UI extends JFrame implements ActionListener
- {
-
-
- JButton jb1,jb2=null;
- JPanel jp1,jp2,jp3=null;
- JLabel jlb1,jlb2,jlb3,jlb4=null;
-
- public static void main(String[] args) {
-
-
- }
-
-
- public UI()
- {
-
- jb1=new JButton("課程管理");
- jb2=new JButton("成績查詢");
-
- jp1=new JPanel();
- jp2=new JPanel();
- jp3=new JPanel();
-
- jlb1=new JLabel("姓名");
- jlb2=new JLabel("學號");
- jlb3=new JLabel("最新公告:");
- jlb4=new JLabel("我校舉行六十週年校慶的通知");
-
- jp1.add(jlb1);
- jp1.add(jlb2);
-
- jp2.add(jb1);
- jp2.add(jlb3);
-
- jp3.add(jb2);
- jp3.add(jlb4);
-
-
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
-
-
- this.setLayout(new GridLayout(3,3,50,50));
- this.setTitle("學生成績管理系統");
- this.setSize(400,300);
- this.setLocation(200, 200);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
-
-
- }
- }
以上程序的執行結果如圖所示:
步驟4:(1)程序鏈接數據庫來進行用戶名和密碼的驗證!!!
(2)驗證成功後進行界面的切換!!!【對步驟三的代碼進行必定的優化】
代碼以下:Login.java
-
-
-
-
-
-
-
-
- package com.package_5;
- import javax.swing.*;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.*;
-
- public class Login extends JFrame implements ActionListener {
-
-
- JButton jb1,jb2,jb3=null;
- JRadioButton jrb1,jrb2=null;
- JPanel jp1,jp2,jp3,jp4=null;
- JTextField jtf=null;
- JLabel jlb1,jlb2,jlb3=null;
- JPasswordField jpf=null;
- ButtonGroup bg=null;
-
-
- JMenuBar jmb=null;
- JMenu jm=null;
- JMenuItem jmi1,jmi2=null;
-
-
- public static void main(String[] args) {
-
- Login ms=new Login();
-
-
- }
-
- public Login()
- {
-
- jb1=new JButton("登陸");
- jb2=new JButton("重置");
- jb3=new JButton("退出");
-
- jb1.addActionListener(this);
- jb2.addActionListener(this);
- jb3.addActionListener(this);
-
- jmb=new JMenuBar();
- jm=new JMenu("選項");
- jmi1=new JMenuItem("開始");
- jmi2=new JMenuItem("退出系統");
- jm.add(jmi1);
- jm.add(jmi2);
- jmb.add(jm);
-
-
- jrb1=new JRadioButton("教師",true);
- jrb2=new JRadioButton("學生");
- bg=new ButtonGroup();
- bg.add(jrb1);
- bg.add(jrb2);
-
-
- jp1=new JPanel();
- jp2=new JPanel();
- jp3=new JPanel();
- jp4=new JPanel();
-
- jlb1=new JLabel("用戶名:");
- jlb2=new JLabel("密 碼:");
- jlb3=new JLabel("權 限:");
-
- jtf=new JTextField(10);
- jpf=new JPasswordField(10);
-
- jp1.add(jlb1);
- jp1.add(jtf);
-
- jp2.add(jlb2);
- jp2.add(jpf);
-
- jp3.add(jlb3);
- jp3.add(jrb1);
- jp3.add(jrb2);
-
- jp4.add(jb1);
- jp4.add(jb2);
- jp4.add(jb3);
-
-
- this.setJMenuBar(jmb);
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
- this.add(jp4);
-
- this.setLayout(new GridLayout(4,1));
-
- this.setTitle("學生成績管理系統");
-
- this.setSize(300,250);
-
- this.setLocation(200, 150);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.setVisible(true);
- this.setResizable(true);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
-
- if(e.getActionCommand()=="退出")
- {
- System.exit(0);
- }else if(e.getActionCommand()=="登陸")
- {
-
- GetSQL.ConnectSQL();
-
- if(jrb1.isSelected())
- {
- GetSQL.sqlquery("教師");
-
- this.tealogin();
- }else if(jrb2.isSelected())
- {
- GetSQL.sqlquery("學生");
-
- this.stulogin();
- }
-
- }else if(e.getActionCommand()=="重置")
- {
- this.clear();
- }
-
- }
-
-
- public void clear()
- {
- jtf.setText("");
- jpf.setText("");
- }
-
- public void stulogin()
- {
- if(GetSQL.userword.equals(jtf.getText())&&GetSQL.pwd.equals(jpf.getText()))
- {
-
- JOptionPane.showMessageDialog(null,"登陸成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- this.clear();
-
- dispose();
-
- Stu_UI ui=new Stu_UI();
- }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名和密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- JOptionPane.showMessageDialog(null,"用戶名或者密碼錯誤!\n請從新輸入","提示消息",JOptionPane.ERROR_MESSAGE);
-
- this.clear();
- }
- }
-
-
- public void tealogin()
- {
- if(GetSQL.userword.equals(jtf.getText())&&GetSQL.pwd.equals(jpf.getText()))
- {
-
- JOptionPane.showMessageDialog(null,"登陸成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- this.clear();
-
- dispose();
-
- Teacher t=new Teacher();
- }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名和密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- JOptionPane.showMessageDialog(null,"用戶名或者密碼錯誤!\n請從新輸入","提示消息",JOptionPane.ERROR_MESSAGE);
-
- this.clear();
- }
- }
-
- }
SQL.java 負責與數據庫創建鏈接,包括倆方法。一個是鏈接數據庫方法,一個是查詢方法。可進一步擴展。
- package com.package_5;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
-
- class GetSQL
- {
-
- static String userword;
- static String pwd;
-
- static Connection ct=null;
- static PreparedStatement ps=null;
- static ResultSet rs=null;
-
-
- public static void ConnectSQL()
- {
- try {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- ct=DriverManager.getConnection("jdbc:odbc:ywq");
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
- }
-
- public static void sqlquery(String s)
- {
-
- try {
- ps=ct.prepareStatement("select * from info where 權限=? ");
-
- ps.setString(1, s);
-
- rs=ps.executeQuery();
-
- while(rs.next()){
-
- userword=rs.getString(2);
- pwd=rs.getString(3);
- System.out.println("成功獲取到密碼和用戶名from數據庫");
- System.out.println(userword+"\t"+pwd+"\t");
- }
- } catch (Exception e1) {
-
- e1.printStackTrace();
- }
- }
- }
Stu_UI.java 學生登陸成功後跳轉的界面
- package com.package_5;
-
- import java.awt.*;
- import java.awt.event.*;
-
- import javax.swing.*;
-
- public class Stu_UI extends JFrame implements ActionListener
- {
-
-
- JButton jb1,jb2=null;
- JPanel jp1,jp2,jp3=null;
- JLabel jlb1,jlb2,jlb3,jlb4=null;
-
-
-
-
-
-
- public Stu_UI()
- {
-
- jb1=new JButton("課程管理");
- jb2=new JButton("成績查詢");
-
- jp1=new JPanel();
- jp2=new JPanel();
- jp3=new JPanel();
-
- jlb1=new JLabel("姓名");
- jlb2=new JLabel("學號");
- jlb3=new JLabel("最新公告:");
- jlb4=new JLabel("我校舉行六十週年校慶的通知");
-
- jp1.add(jlb1);
- jp1.add(jlb2);
-
- jp2.add(jb1);
- jp2.add(jlb3);
-
- jp3.add(jb2);
- jp3.add(jlb4);
-
-
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
-
-
- this.setLayout(new GridLayout(3,3,50,50));
- this.setTitle("學生成績管理系統");
- this.setSize(400,300);
- this.setLocation(200, 200);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
-
-
- }
- }
Teacher.java 教師登陸成功後跳轉的界面。
- package com.package_5;
-
- import javax.swing.*;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class Teacher extends JFrame implements ActionListener{
-
-
- JLabel jl2=null;
- JTextField jtf=null;
- JButton jb=null;
- JPanel jp1,jp2=null;
- JTable jtable=null;
-
- String name=null;
- String num=null;
-
- static Connection ct=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Teacher()
- {
-
- jl2=new JLabel("請輸入學號:");
- jtf=new JTextField(10);
- jb=new JButton("查詢");
-
- jb.addActionListener(this);
-
- final Object[] columnNames = {"姓名","學號"};
- Object[][] rowData = {
- {"小明","2015110512"},
- {"小","2015110"},
- {"小紅","2015110511"}
- };
- jtable=new JTable(rowData, columnNames);
-
- jp1=new JPanel();
- jp2=new JPanel();
-
- jp1.add(jl2);
- jp1.add(jtf);
- jp1.add(jb);
- jp2.add(jtable);
-
- this.add(jp1);
- this.add(jp2);
-
- this.setLayout(new GridLayout(2,3));
- this.setTitle("學生成績管理系統—教師");
- this.setSize(500,400);
- this.setLocation(200, 200);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
-
-
- }
-
-
- @Override
- public void actionPerformed(ActionEvent e) {
-
- if(e.getActionCommand()=="查詢")
- {
-
-
- try {
- ps=ct.prepareStatement("select * from info where xuehao=? ");
-
- ps.setString(1, jtf.getText());
-
- rs=ps.executeQuery();
-
- if(rs.next())
- {
- name=rs.getString(5);
- System.out.println("查詢成功from數據庫");
- System.out.println(name+"\t");
- JOptionPane.showMessageDialog(null,"查詢成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- System.out.println("查詢失敗");
- JOptionPane.showMessageDialog(null,"查詢失敗!","提示消息",JOptionPane.WARNING_MESSAGE);
- }
-
-
-
-
-
-
-
- } catch (SQLException e1) {
-
- e1.printStackTrace();
- }
-
-
- }
-
- }
- }
步驟5:優化數據庫驗證條件,使用不一樣的表來進行查詢。
教師界面增長查詢功能。
代碼以下:Login6.java
-
-
-
-
-
-
-
-
-
-
-
- package com.package_7;
- import javax.swing.*;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.*;
-
- public class Login6 extends JFrame implements ActionListener {
-
-
- JButton jb1,jb2,jb3=null;
- JRadioButton jrb1,jrb2=null;
- JPanel jp1,jp2,jp3,jp4=null;
- JTextField jtf=null;
- JLabel jlb1,jlb2,jlb3=null;
- JPasswordField jpf=null;
- ButtonGroup bg=null;
-
-
- JMenuBar jmb=null;
- JMenu jm=null;
- JMenuItem jmi1,jmi2=null;
-
-
- public static void main(String[] args) {
-
- Login6 ms=new Login6();
-
-
- }
-
- public Login6()
- {
-
- jb1=new JButton("登陸");
- jb2=new JButton("重置");
- jb3=new JButton("退出");
-
- jb1.addActionListener(this);
- jb2.addActionListener(this);
- jb3.addActionListener(this);
-
- jmb=new JMenuBar();
- jm=new JMenu("選項");
- jmi1=new JMenuItem("開始");
- jmi2=new JMenuItem("退出系統");
- jm.add(jmi1);
- jm.add(jmi2);
- jmb.add(jm);
-
-
- jrb1=new JRadioButton("教師",true);
- jrb2=new JRadioButton("學生");
- bg=new ButtonGroup();
- bg.add(jrb1);
- bg.add(jrb2);
-
-
- jp1=new JPanel();
- jp2=new JPanel();
- jp3=new JPanel();
- jp4=new JPanel();
-
- jlb1=new JLabel("用戶名:");
- jlb2=new JLabel("密 碼:");
- jlb3=new JLabel("權 限:");
-
- jtf=new JTextField(10);
- jpf=new JPasswordField(10);
-
- jp1.add(jlb1);
- jp1.add(jtf);
-
- jp2.add(jlb2);
- jp2.add(jpf);
-
- jp3.add(jlb3);
- jp3.add(jrb1);
- jp3.add(jrb2);
-
- jp4.add(jb1);
- jp4.add(jb2);
- jp4.add(jb3);
-
-
- this.setJMenuBar(jmb);
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
- this.add(jp4);
-
- this.setLayout(new GridLayout(4,1));
-
- this.setTitle("學生成績管理系統");
-
- this.setSize(300,250);
-
- this.setLocation(200, 150);
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- this.setVisible(true);
- this.setResizable(true);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
-
- if(e.getActionCommand()=="退出")
- {
- System.exit(0);
- }else if(e.getActionCommand()=="登陸")
- {
- if(!jtf.getText().isEmpty() && !jpf.getText().isEmpty())
- {
-
- GetSQL.ConnectSQL();
-
- if(jrb1.isSelected())
- {
- GetSQL.querytea("教師",jtf.getText());
-
- if(GetSQL.pwd ==null)
- {
- this.clear();
- }else
- {
-
- this.tealogin();
- }
- }else if(jrb2.isSelected())
- {
- GetSQL.querystu("學生",jtf.getText());
-
- if(GetSQL.pwd ==null)
- {
- this.clear();
- }else
- {
-
- this.stulogin();
- }
-
- }
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名","提示消息",JOptionPane.WARNING_MESSAGE);
- this.clear();
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼","提示消息",JOptionPane.WARNING_MESSAGE);
- this.clear();
- }
- }else if(e.getActionCommand()=="重置")
- {
- this.clear();
- }
-
- }
-
-
- public void clear()
- {
- jtf.setText("");
- jpf.setText("");
- }
-
- public void stulogin()
- {
- if(GetSQL.pwd.equals(jpf.getText()))
- {
-
- JOptionPane.showMessageDialog(null,"登陸成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- this.clear();
-
- dispose();
-
- Stu_UI6 ui=new Stu_UI6();
- }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名和密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- JOptionPane.showMessageDialog(null,"用戶名或者密碼錯誤!\n請從新輸入","提示消息",JOptionPane.ERROR_MESSAGE);
-
- this.clear();
- }
- }
-
-
- public void tealogin()
- {
- if(GetSQL.pwd.equals(jpf.getText()))
- {
-
- JOptionPane.showMessageDialog(null,"登陸成功!","提示消息",JOptionPane.WARNING_MESSAGE);
- this.clear();
-
- dispose();
-
- Teacher6 t=new Teacher6();
- }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名和密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jtf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入用戶名!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else if(jpf.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(null,"請輸入密碼!","提示消息",JOptionPane.WARNING_MESSAGE);
- }else
- {
- JOptionPane.showMessageDialog(null,"用戶名或者密碼錯誤!\n請從新輸入","提示消息",JOptionPane.ERROR_MESSAGE);
-
- this.clear();
- }
- }
-
- }
SQL.java
-
-
-
- package com.package_7;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import javax.swing.JOptionPane;
-
-
- class GetSQL {
-
- static String userword;
- static String pwd;
-
- static String english;
- static String num;
- static String name;
- static String chinese;
- static String zhengzhi;
- static String math;
-
- static String age;
- static String salary;
- static String sex;
- static String zhicheng;
- static String teanum;
- static String teaname;
-
- static Connection ct = null;
- static PreparedStatement ps = null;
- static ResultSet rs = null;
-
-
- public static void ConnectSQL() {
- try {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- ct = DriverManager.getConnection("jdbc:odbc:ywq");
- System.out.println("The SQL is connected");
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
- }
-
-
- public static void querystu(String s,String username) {
-
- try {
- ps = ct.prepareStatement("select * from info where 權限=? and 用戶名=? ");
-
- ps.setString(1, s);
- ps.setString(2, username);
-
- rs = ps.executeQuery();
-
- if (rs.next()) {
-
- userword = rs.getString(2);
- pwd = rs.getString(3);
- System.out.println("成功獲取到密碼和用戶名from數據庫");
- System.out.println(userword + "\t" + pwd + "\t");
- }else
- {
- JOptionPane.showMessageDialog(null, "沒有此用戶,請從新輸入!", "提示消息", JOptionPane.WARNING_MESSAGE);
- }
- } catch (Exception e1) {
-
- e1.printStackTrace();
- }
- }
-
- public static void querytea(String s,String name ) {
-
- try {
- ps = ct.prepareStatement("select * from info_tea where 權限=? and 用戶名=? ");
-
- ps.setString(1, s);
- ps.setString(2, name);
-
- rs = ps.executeQuery();
-
- if (rs.next()) {
-
- userword = rs.getString(2);
- pwd = rs.getString(3);
- System.out.println("成功獲取到密碼和用戶名from數據庫");
- System.out.println(userword + "\t" + pwd + "\t");
- }else
- {
- JOptionPane.showMessageDialog(null, "沒有此用戶,請從新輸入!", "提示消息", JOptionPane.WARNING_MESSAGE);
- }
- } catch (Exception e1) {
-
- e1.printStackTrace();
- }
- }
-
-
- public static void getdatastu(String s) {
-
- try {
- ps = ct.prepareStatement("select * from info where 學號 =? ");
-
- ps.setString(1, s);
-
- rs = ps.executeQuery();
- if(rs.next())
- {
-
- num = rs.getString(4);
- name = rs.getString(5);
- math = rs.getString(6);
- chinese = rs.getString(7);
- english = rs.getString(8);
- zhengzhi = rs.getString(9);
- }else
- {
- JOptionPane.showMessageDialog(null, "沒有此學生,請從新輸入", "提示消息", JOptionPane.WARNING_MESSAGE);
- }
-
- } catch (Exception e1) {
-
- e1.printStackTrace();
- }
- }
- public static void getdatatea(String s) {
-
- try {
- ps = ct.prepareStatement("select * from info_tea where 教師編號號 =? ");
-
- ps.setString(1, s);
-
- rs = ps.executeQuery();
- if(rs.next())
- {
-
- teanum = rs.getString(4);
- teaname = rs.getString(5);
- sex = rs.getString(6);
- salary = rs.getString(7);
- zhicheng = rs.getString(8);
- age = rs.getString(9);
- }else
- {
- JOptionPane.showMessageDialog(null, "沒有此教師,請從新輸入", "提示消息", JOptionPane.WARNING_MESSAGE);
- }
-
- } catch (Exception e1) {
-
- e1.printStackTrace();
- }
- }
-
- }
Stu_UI6.java
-
-
-
-
- package com.package_7;
-
- import java.awt.*;
- import java.awt.event.*;
-
- import javax.swing.*;
-
- public class Stu_UI6 extends JFrame implements ActionListener
- {
-
-
- JButton jb1,jb2=null;
- JPanel jp1,jp2,jp3=null;
- JLabel jlb1,jlb2,jlb3,jlb4=null;
-
-
-
-
-
-
- public Stu_UI6()
- {
-
- jb1=new JButton("課程管理");
- jb2=new JButton("成績查詢");
-
- jp1=new JPanel();
- jp2=new JPanel();
- jp3=new JPanel();
-
- jlb1=new JLabel("姓名");
- jlb2=new JLabel("學號");
- jlb3=new JLabel("最新公告:");
- jlb4=new JLabel("我校舉行六十週年校慶的通知");
-
- jp1.add(jlb1);
- jp1.add(jlb2);
-
- jp2.add(jb1);
- jp2.add(jlb3);
-
- jp3.add(jb2);
- jp3.add(jlb4);
-
-
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
-
-
- this.setLayout(new GridLayout(3,3,50,50));
- this.setTitle("學生成績管理系統");
- this.setSize(400,300);
- this.setLocation(200, 200);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
-
-
- }
- }
Teacher.java
-
-
-
-
- package com.package_7;
-
- import javax.swing.*;
- import javax.swing.table.DefaultTableModel;
-
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class Teacher6 extends JFrame implements ActionListener {
-
-
- JLabel jl2,jl,jl3,jl4 = null;
- JTextField jtf,jtf2 = null;
- JButton jb,jb2 = null;
- JPanel jp1, jp2,jp3,jp4,jp5,jp6 = null;
-
- DefaultTableModel model,model2 = null;
- JTable table,table2 = null;
- JScrollPane jsp,jsp2 = null;
-
-
-
-
-
-
-
- public Teacher6() {
-
- jl = new JLabel("請輸入學號:");
- jl2=new JLabel("請輸入教工號:");
-
- jl3=new JLabel("學生信息表:");
- jl4=new JLabel("教師信息表:");
-
- jtf = new JTextField(10);
- jtf2 = new JTextField(10);
- jb = new JButton("查詢1");
- jb2 = new JButton("查詢2");
-
- jb.addActionListener(this);
- jb2.addActionListener(this);
-