標籤:rri 函數 als 圖形 查詢 student 密碼 rac mes
學生資訊系統介面的實現 - JDBCwriter: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練習 - 學生資訊系統介面