先上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);


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |