IT笔记 java 大一下java期末设计:学生信息管理系统(荣获班上第一) 何平安 2024-02-03 2024-08-03 其实就是当时我们没教数据库然后我会JDBC然后就得了第一了,一堆if判断语句,连个图形化界面都没有,现在看来还是感觉太低级了…–by 2024-2-7
从此之后就一直是期末设计第一了 –by 2024-8-2
先上java代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 先上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) {System.out.println("欢迎使用学生信息管理系统!" ); System.out.println("正在为你加载程序..." ); final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver" ;final String DB_URL = "jdbc:mysql://localhost:3306/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 = ?" ; 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);