ToB企服应用市场:ToB评测及商务社交产业平台

标题: 大一下java期末设计:学生信息管理系统(荣获班上第一) [打印本页]

作者: 惊雷无声    时间: 2023-7-14 21:47
标题: 大一下java期末设计:学生信息管理系统(荣获班上第一)
先上java代码:
  1. 先上java代码:
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.*;
  6. import java.util.Scanner;
  7. import javax.swing.*;
  8. public class cs2 extends JFrame {
  9. public static void main(String[] args) {
  10. // 未完成的swing界面设计
  11. // JFrame frame = new JFrame("学生信息管理系统");
  12. // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. // frame.setSize(600,400);
  14. // frame.setVisible(true);
  15. // frame.setLayout(new FlowLayout());
  16. // JButton loginbutton=new JButton("登录");
  17. // frame.add(loginbutton);
  18. // JButton zhucebutton=new JButton("注册");
  19. // frame.add(zhucebutton);
  20. // JButton checkBot= new JButton("查询");
  21. // checkBot.setEnabled(false);
  22. // JButton luruBot =new JButton("录入");
  23. // luruBot.setEnabled(false);
  24. // frame.add(checkBot);
  25. // frame.add(luruBot);
  26. // ImageIcon imageIcon =new ImageIcon(("src/hehao.jpg"));
  27. //
  28. // JPanel loginPanel =new JPanel();
  29. // loginPanel.setLayout(new GridLayout(3,2));
  30. // loginPanel.add(new JLabel("姓名:"));
  31. // JTextField usernameField=new JTextField();
  32. // loginPanel.add(new JLabel("密码:"));
  33. // JTextField PasswordField= new JTextField();
  34. // loginPanel.add(PasswordField);
  35. // loginPanel.add(loginbutton);
  36. // loginPanel.add(zhucebutton);
  37. //
  38. // loginbutton.addActionListener(new ActionListener() {
  39. // @Override
  40. // public void actionPerformed(ActionEvent e) {
  41. // String username = usernameField.getText();
  42. // String password = String.valueOf(PasswordField.getText());
  43. // if(username.equals("name")&&password.equals("password")){
  44. // loginbutton.setVisible(false);
  45. // luruBot.setEnabled(true);
  46. // checkBot.setEnabled(true);
  47. // }else{
  48. // JOptionPane.showMessageDialog(frame,
  49. // "用户名或密码输入错误",
  50. // "登录失败",
  51. // JOptionPane.ERROR_MESSAGE);
  52. // }
  53. // }
  54. // });
  55. // zhucebutton.addActionListener(new ActionListener() {
  56. // @Override
  57. // public void actionPerformed(ActionEvent e) {
  58. // loginbutton.setEnabled(false);
  59. // }
  60. // });
  61. // SwingUtilities.invokeLater(new Runnable() {
  62. // @Override
  63. // public void run() {
  64. // new cs2();
  65. // }
  66. // });2
  67. System.out.println("欢迎使用学生信息管理系统!");
  68. System.out.println("正在为你加载程序...");
  69. final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
  70. final String DB_URL = "jdbc:mysql://localhost:3306/hh";//数据库名为hh
  71. final String USER = "root";
  72. final String PASS = "123cd233";//账号和密码
  73. System.out.println("请选择你的操作:登录按1,注册账号按2");
  74. Scanner input = new Scanner(System.in);
  75. int x = input.nextInt();
  76. int y = 0;
  77. Connection conn = null;
  78. ResultSet rs = null;
  79. PreparedStatement pstmt = null;
  80. Statement stmt = null;
  81. if (x != 1 && x != 2) {
  82. System.out.println("您输入的有误,请重新运行程序");
  83. System.exit(0);
  84. }
  85. zhuce:
  86. while (x == 2) {
  87. System.out.println("您已进入注册系统");
  88. try {
  89. Class.forName(JDBC_DRIVER);
  90. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  91. Scanner scanner = new Scanner(System.in);
  92. System.out.println("请输入你的学号:");
  93. int id = scanner.nextInt();
  94. System.out.println("请输入你的姓名:");
  95. String name = scanner.next();
  96. System.out.println("请输入你的注册的密码:");
  97. String password = scanner.next();
  98. String sql2 ="SELECT * FROM student WHERE id=? ";
  99. pstmt=conn.prepareStatement(sql2);
  100. pstmt.setInt(1,id);
  101. String sql = "INSERT INTO student (id, name, password) VALUES (?, ?, ?)";
  102. pstmt = conn.prepareStatement(sql);
  103. pstmt.setInt(1, id);
  104. pstmt.setString(2, name);
  105. pstmt.setString(3, password);
  106. int rows = pstmt.executeUpdate();
  107. if (rows > 0) {
  108. System.out.println("注册成功!");
  109. }
  110. } catch (Exception se) {
  111. se.printStackTrace();
  112. } finally {
  113. try {
  114. if (pstmt != null) pstmt.close();
  115. if (conn != null) conn.close();
  116. } catch (Exception se) {
  117. se.printStackTrace();
  118. }
  119. }
  120. x = -1;
  121. }
  122. denglu:
  123. while (x == 1) {
  124. System.out.println("您已进入登录系统");
  125. try {
  126. Class.forName(JDBC_DRIVER);
  127. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  128. Scanner scanner = new Scanner(System.in);
  129. System.out.println("请输入你的学号:");
  130. int id = scanner.nextInt();
  131. System.out.println("请输入你姓名:");
  132. String name = scanner.next();
  133. System.out.println("请输入你的密码:");
  134. String password = scanner.next();
  135. String sql = "SELECT * FROM student WHERE id = ? and name = ? and password = ?";
  136. pstmt = conn.prepareStatement(sql);
  137. pstmt.setInt(1, id);
  138. pstmt.setString(2, name);
  139. pstmt.setString(3, password);
  140. rs = pstmt.executeQuery();
  141. if (rs.next()) {
  142. System.out.println("输入成功.");
  143. y = 1;
  144. } else {
  145. System.out.println("你输入的有误,已为你重新运行登录系统请再次输入.");
  146. y = 2;
  147. }
  148. } catch (Exception se) {
  149. se.printStackTrace();
  150. } finally {
  151. try {
  152. if (rs != null) rs.close();
  153. if (pstmt != null) pstmt.close();
  154. if (conn != null) conn.close();
  155. } catch (Exception se) {
  156. se.printStackTrace();
  157. }
  158. if (y == 1) {
  159. break;
  160. } else if (y == 2) {
  161. continue;
  162. }
  163. }
  164. }
  165. if (y == 1) {
  166. System.out.println("--------------------");
  167. System.out.println("欢迎进入学生信息管理系统");
  168. System.out.println("---------------------");
  169. System.out.println("请选择你的操作:查询成绩请按1,录入成绩请按2");
  170. int r = input.nextInt();
  171. if (r == 2) {
  172. System.out.println("请输入你的成绩:");
  173. try {
  174. Class.forName(JDBC_DRIVER);
  175. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  176. Scanner scanner = new Scanner(System.in);
  177. int score = scanner.nextInt();
  178. System.out.println("请输入你的学号:");
  179. int id = scanner.nextInt();
  180. String sql = "UPDATE student SET score= ? WHERE id=?";
  181. pstmt = conn.prepareStatement(sql);
  182. pstmt.setInt(1, score);
  183. pstmt.setInt(2, id);
  184. int rows = pstmt.executeUpdate();
  185. if (rows > 0) {
  186. System.out.println("录入成功!");
  187. }
  188. } catch (Exception se) {
  189. se.printStackTrace();
  190. } finally {
  191. try {
  192. if (pstmt != null) pstmt.close();
  193. if (conn != null) conn.close();
  194. } catch (Exception se) {
  195. se.printStackTrace();
  196. }
  197. }
  198. }
  199. if (r == 1) {
  200. System.out.println("请输入你的学号:");
  201. try {
  202. Class.forName(JDBC_DRIVER);
  203. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  204. int id = input.nextInt();
  205. String sql = "SELECT score FROM student WHERE id = ?"; // Use placeholders
  206. pstmt = conn.prepareStatement(sql);
  207. pstmt.setInt(1, id);
  208. rs = pstmt.executeQuery();
  209. if(rs.next()) {
  210. int score = rs.getInt("score");
  211. System.out.println("成绩为: " + score+" , tips:如果未录入成绩默认成绩为0分");
  212. }
  213. input.close();
  214. } catch (SQLException se) {
  215. se.printStackTrace();
  216. } catch (Exception e) {
  217. e.printStackTrace();
  218. } finally {
  219. try {
  220. if (rs != null)
  221. rs.close();
  222. } catch (SQLException se) {
  223. }
  224. try {
  225. if (pstmt != null)
  226. pstmt.close();
  227. } catch (SQLException se) {
  228. }
  229. try {
  230. if (conn != null)
  231. conn.close();
  232. } catch (SQLException se) {
  233. se.printStackTrace();
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. 分享一个IDEA的破解网站教程:IDEA2023.1破解 永久激活 最新版IDEA激活 亲测可用! – 爱激活网 (aijihuo.cn)
  241. 数据使用的mysql数据库,版本是8.1,创建student表的数据库代码:
  242. 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);



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




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4