首页 > 编程语言 >大一下java期末设计:学生信息管理系统(荣获班上第一)

大一下java期末设计:学生信息管理系统(荣获班上第一)

时间:2023-07-14 21:56:48浏览次数:44  
标签:java System 班上 new println 信息管理系统 out conn pstmt

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

标签:java,System,班上,new,println,信息管理系统,out,conn,pstmt
From: https://www.cnblogs.com/hepingan/p/17555086.html

相关文章

  • 学科知识图谱学习平台项目 :技术栈Java、Neo4j、MySQL等超详细教学
    学科知识图谱学习平台项目:技术栈Java、Neo4j、MySQL等超详细教学0.效果展示1.安装教程安装JavaSDK11,下载前需要登录Oracle账号,下载链接,安装教程,测试是否能在命令行工具调用javajava--versionjava17.0.12021-10-19LTSJava(TM)SERuntimeEnvironment(build......
  • java8 LocalData/Time
    ISO_DATE_TIMELStringtimeString=LocalDateTime.now().atOffset(ZoneOffset.ofHours(8)).format(DateTimeFormatter.ISO_DATE_TIME);System.out.println(timeString);//2023-07-14T18:28:23.056+08:00增加与减小LocalDatedate=LocalDate.now().minusMonths(1);LocalDa......
  • Java POM Dependency
     <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">......
  • java拦截器获取POST请求体后Controller异常Required request body is missing OR Stre
    解决办法参考文档:https://blog.csdn.net/qierkang/article/details/88544691springboot拦截器获取POST请求体后导致Controller中@RequestBody参数异常RequiredrequestbodyismissingORStreamclosed.1.为什么会报这个错?因为http的body只能读取一次。2.为什么body设计为只......
  • Java入门13(socket)
    Socket编程(网络通信)服务器端Demo(ServreSocket)​ 创建服务端时,如果不提供IP地址,则默认为本地连接(127.0.0.1),但是一定需要手动配置监听端口!publicstaticvoidmain(String[]args){//如果不提供IP地址,默认localhost//但是服务器端的端口号需要手动指令try(Serv......
  • java--String类的常用方法
    一、获取1、length()  获取字符串长度Stringstr="ahcckmvevawe";System.out.println(str.length());//输出122、charAt(intindex)  返回下标对应的字符Stringstr="ahcckmvevawe";System.out.println(str.charAt(4));//输出k3、indexOf()  返回字符对......
  • JavaWeb基础:安装tomcat和maven
    JavaWeb基础:安装tomcat和maventomcat闪退问题下载zip即可,无需配置,只需运行相关文件即可文档所在位置:D:\Environment\apache-tomcat-9.0.78\bin开启:startup.bat关闭:shutdown.batjava8不适用tamcat10及以上版本解决办法:https://blog.csdn.net/egegerhn/article/details/1260......
  • java 跨域
       ......
  • JavaScript at() 方法
    数组对象:对于获取数组的最后一个元素,可能平常见得多的就是arr[arr.length-1],我们其实可以使用at()方法进行获取接收一个整数值并返回该索引对应的元素:constarr=[5,12,8,130,44];letindex1=2;strt1=`索引号为${index1}的值为${arr.at(index1)}`;letind......
  • 学习Java第2天
    ##快捷键Ctrl+c复制Ctrl+v粘贴Ctrl+z撤销Ctrl+a全选Ctrl+x剪切Ctrl+s保存Alt+f4关闭窗口Windows+e我的电脑Windows+r运行窗口Ctrl+shift+ESC任务管理器#打开CMD的方式1.开始+系统+命令提示符2.win健+r输入cmd打开控制台(推荐使用)3.在任意的文件夹......