java学生管理系统,单文件最简版(附源码文件)
升级终极版已经写完了点击查看https://blog.csdn.net/weixin_43419816/article/details/104257185
最后附有网盘链接(程序打包+数据库)
只在一个主方法文件里面完成,比较简陋
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import Util.DbUtil;
public class Main {
public static void main(String[] args) {
showMannue();
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
switch (i) {
case 1:
System.out.println("请输入学生学号");
String stu_no = scanner.next();
System.out.println("请输入学生姓名");
String stu_name = scanner.next();
intPut(stu_no, stu_name);
break;
case 2:
System.out.println("请输入学生学号");
stu_no = scanner.next();
delete(stu_no);
break;
case 3:
System.out.println("请输入学生学号");
stu_no = scanner.next();
System.out.println("请输入新的姓名");
stu_name = scanner.next();
changeName(stu_no, stu_name);
break;
case 4:
query();
}
}
public static void intPut(String stu_no, String stu_name) {
Connection conn = DbUtil.getDBConn();
Statement stat = null;
String sql = "insert into student values('"+stu_no+"','"+stu_name+"' )";
try {
stat = conn.createStatement();
int affectedRow = stat.executeUpdate(sql);
if(affectedRow > 0) {
System.out.println("添加成功!!!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DbUtil.close(conn);
DbUtil.close(stat);
}
}
public static void delete(String stu_no) {
Connection conn = DbUtil.getDBConn();
Statement stat = null;
String sql = "delete from student where stu_no = '"+stu_no+"'";
try {
stat = conn.createStatement();
int affectedRow = stat.executeUpdate(sql);
if(affectedRow > 0) {
System.out.println("删除成功!!!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DbUtil.close(conn);
DbUtil.close(stat);
}
}
public static void changeName(String stu_no, String newName) {
Connection conn = DbUtil.getDBConn();
Statement stat = null;
String sql = "update student set stu_name = '"+newName+"' where stu_no = '"+stu_no+"'";
try {
stat = conn.createStatement();
int affectedRow = stat.executeUpdate(sql);
if(affectedRow > 0) {
System.out.println("修改成功!!!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DbUtil.close(conn);
DbUtil.close(stat);
}
}
public static void query() {
Connection conn = DbUtil.getDBConn();
Statement stat = null;
ResultSet rs = null;
String sql = "select * from student";
try {
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DbUtil.close(conn);
DbUtil.close(stat);
DbUtil.close(rs);
}
}
public static void showMannue() {
System.out.println("请选择要使用的功能:");
System.out.println("1、添加学生信息");
System.out.println("2、删除学生信息");
System.out.println("3、修改学生信息");
System.out.println("4、查看学生信息");
}
}
链接
链接:https://pan.baidu.com/s/1_8Hs6V06GdX7ia5fYV4MlQ
提取码:7s8u