先上java代碼: 先上java代碼: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; import java.util.Sc ...
先上java代碼:
先上java代碼: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; import java.util.Scanner; import javax.swing.*; public class cs2 extends JFrame { public static void main(String[] args) { // 未完成的swing界面設計 // JFrame frame = new JFrame("學生信息管理系統"); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // frame.setSize(600,400); // frame.setVisible(true); // frame.setLayout(new FlowLayout()); // JButton loginbutton=new JButton("登錄"); // frame.add(loginbutton); // JButton zhucebutton=new JButton("註冊"); // frame.add(zhucebutton); // JButton checkBot= new JButton("查詢"); // checkBot.setEnabled(false); // JButton luruBot =new JButton("錄入"); // luruBot.setEnabled(false); // frame.add(checkBot); // frame.add(luruBot); // ImageIcon imageIcon =new ImageIcon(("src/hehao.jpg")); // // JPanel loginPanel =new JPanel(); // loginPanel.setLayout(new GridLayout(3,2)); // loginPanel.add(new JLabel("姓名:")); // JTextField usernameField=new JTextField(); // loginPanel.add(new JLabel("密碼:")); // JTextField PasswordField= new JTextField(); // loginPanel.add(PasswordField); // loginPanel.add(loginbutton); // loginPanel.add(zhucebutton); // // loginbutton.addActionListener(new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // String username = usernameField.getText(); // String password = String.valueOf(PasswordField.getText()); // if(username.equals("name")&&password.equals("password")){ // loginbutton.setVisible(false); // luruBot.setEnabled(true); // checkBot.setEnabled(true); // }else{ // JOptionPane.showMessageDialog(frame, // "用戶名或密碼輸入錯誤", // "登錄失敗", // JOptionPane.ERROR_MESSAGE); // } // } // }); // zhucebutton.addActionListener(new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // loginbutton.setEnabled(false); // } // }); // SwingUtilities.invokeLater(new Runnable() { // @Override // public void run() { // new cs2(); // } // });2 System.out.println("歡迎使用學生信息管理系統!"); System.out.println("正在為你載入程式..."); final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; final String DB_URL = "jdbc:mysql://localhost:3306/hh";//資料庫名為hh final String USER = "root"; final String PASS = "123cd233";//賬號和密碼 System.out.println("請選擇你的操作:登錄按1,註冊賬號按2"); Scanner input = new Scanner(System.in); int x = input.nextInt(); int y = 0; Connection conn = null; ResultSet rs = null; PreparedStatement pstmt = null; Statement stmt = null; if (x != 1 && x != 2) { System.out.println("您輸入的有誤,請重新運行程式"); System.exit(0); } zhuce: while (x == 2) { System.out.println("您已進入註冊系統"); try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); Scanner scanner = new Scanner(System.in); System.out.println("請輸入你的學號:"); int id = scanner.nextInt(); System.out.println("請輸入你的姓名:"); String name = scanner.next(); System.out.println("請輸入你的註冊的密碼:"); String password = scanner.next(); String sql2 ="SELECT * FROM student WHERE id=? "; pstmt=conn.prepareStatement(sql2); pstmt.setInt(1,id); String sql = "INSERT INTO student (id, name, password) VALUES (?, ?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, name); pstmt.setString(3, password); int rows = pstmt.executeUpdate(); if (rows > 0) { System.out.println("註冊成功!"); } } catch (Exception se) { se.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception se) { se.printStackTrace(); } } x = -1; } denglu: while (x == 1) { System.out.println("您已進入登錄系統"); try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); Scanner scanner = new Scanner(System.in); System.out.println("請輸入你的學號:"); int id = scanner.nextInt(); System.out.println("請輸入你姓名:"); String name = scanner.next(); System.out.println("請輸入你的密碼:"); String password = scanner.next(); String sql = "SELECT * FROM student WHERE id = ? and name = ? and password = ?"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, name); pstmt.setString(3, password); rs = pstmt.executeQuery(); if (rs.next()) { System.out.println("輸入成功."); y = 1; } else { System.out.println("你輸入的有誤,已為你重新運行登錄系統請再次輸入."); y = 2; } } catch (Exception se) { se.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception se) { se.printStackTrace(); } if (y == 1) { break; } else if (y == 2) { continue; } } } if (y == 1) { System.out.println("--------------------"); System.out.println("歡迎進入學生信息管理系統"); System.out.println("---------------------"); System.out.println("請選擇你的操作:查詢成績請按1,錄入成績請按2"); int r = input.nextInt(); if (r == 2) { System.out.println("請輸入你的成績:"); try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); System.out.println("請輸入你的學號:"); int id = scanner.nextInt(); String sql = "UPDATE student SET score= ? WHERE id=?"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, score); pstmt.setInt(2, id); int rows = pstmt.executeUpdate(); if (rows > 0) { System.out.println("錄入成功!"); } } catch (Exception se) { se.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception se) { se.printStackTrace(); } } } if (r == 1) { System.out.println("請輸入你的學號:"); try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL,USER,PASS); int id = input.nextInt(); String sql = "SELECT score FROM student WHERE id = ?"; // Use placeholders pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); rs = pstmt.executeQuery(); if(rs.next()) { int score = rs.getInt("score"); System.out.println("成績為: " + score+" , tips:如果未錄入成績預設成績為0分"); } input.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); } catch (SQLException se) { } try { if (pstmt != null) pstmt.close(); } catch (SQLException se) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } } } } } 分享一個IDEA的破解網站教程:IDEA2023.1破解 永久激活 最新版IDEA激活 親測可用! – 愛激活網 (aijihuo.cn) 數據使用的mysql資料庫,版本是8.1,創建student表的資料庫代碼: create table student(id int name varchar(20),password varchar(20),score int);
分享一個IDEA的破解網站教程:IDEA2023.1破解 永久激活 最新版IDEA激活 親測可用! – 愛激活網 (aijihuo.cn)
數據使用的mysql資料庫,版本是8.1,創建student表的資料庫代碼:
create table student(id int name varchar(20),password varchar(20),score int);