基于JDBC的学生管理系统

如何使用JDBC连接数据库完成学生管理系统。
数据库设计(可以根据需要自行设计)如下图
在这里插入图片描述
插入学生数据:
public static Boolean addStu(String id, String name, String sex){
try {
conn = DBUtils.getConn();
String sql = “insert into student values(?,?,?)”;
stat = conn.prepareStatement(sql);
stat.setString(1, id);
stat.setString(2, name);
stat.setString(3, sex);
boolean rs =stat.execute();
if(!rs){
System.out.println(“学号:”+id+" 姓名:"+name+" 性别:"+sex);
}
return rs;
}catch (SQLException e) {
System.out.println(“输入的学号重复!”);
}finally {
DBUtils.close(stat,conn);
}
return true;
}

根据学生学号查询学生信息:
public static boolean selectStu(String id){
try {
conn = DBUtils.getConn();
String sql = “select * from student where id = ? “;
stat = conn.prepareStatement(sql);
stat.setString(1, id);
rs =stat.executeQuery();
boolean ss=rs.next();
if(ss){
String name = rs.getString(“name”);
String sex = rs.getString(“sex”);
System.out.println(“学号:”+id+” 姓名:”+name+" 性别:"+sex);
}
if (!ss){
System.out.println(“输入的学号不存在!”);
}
return ss;
} catch (SQLException e) {
//e.printStackTrace();
}finally {
DBUtils.close(stat,conn);
}
return false;
}

修改学生的信息:
public static void updateStu(String id){
try {
conn = DBUtils.getConn();
String sql = "update student set name = ? , sex = ? where id = ? ";
if(selectStu(id)){
System.out.println(“输入修改后的学生姓名:”);
String name = sc.next();
System.out.println(“输入修改后的学生性别:”);
String sex = sc.next();
stat = conn.prepareStatement(sql);
stat.setString(3, id);
stat.setString(1, name);
stat.setString(2, sex);
boolean rs =stat.execute();
System.out.print(“修改后的学生信息:”);
selectStu(id);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.close(stat,conn);
}
}

根据学生学号删除学生信息:
private static void deleteStu(String id) {
try {
conn = DBUtils.getConn();
String sql = "delete from student where id = ? ";
String sql2 = "select * from student where id = ? ";
stat = conn.prepareStatement(sql2);
stat.setString(1, id);
rs = stat.executeQuery();
if(rs.next()){
stat = conn.prepareStatement(sql);
stat.setString(1, id);
stat.execute();
System.out.println(“删除成功!”);
}else{
System.out.println(“输入的学号不存在!”);
}
} catch (SQLException e) {
e.printStackTrace();
// System.out.println(“输入的学号不存在!”);
}finally {
DBUtils.close(stat,conn);
}
}

打印所以学生的信息:
private static void outputStu(){
try {
conn = DBUtils.getConn();
String sql = “select * from student”;
stat = conn.prepareStatement(sql);
rs =stat.executeQuery();

        while(rs.next()){
            String id = rs.getString("id");
            String name = rs.getString("name");
            String sex = rs.getString("sex");
            System.out.println("学号:"+id+" 姓名:"+name+" 性别:"+sex);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        DBUtils.close(stat,conn);
    }
}

主函数:
public static void main(String[] args) {
boolean f = true;
while(f){
System.out.println("**** 高校学院管理系统 ****");
System.out.println(“请选择你需要进行的操作1、添加学生信息2、查询学生信息,3、修改学生信息,4、删除学生信息,5、打印学生信息,6、退出”);
int x = sc.nextInt();
boolean flag;
String name;
String id;
String sex;
if(x>6||x<1)
System.out.println(“输入错误!”);
switch (x){
case 1:
System.out.println(“输入学生姓名:”);
name = sc.next();
System.out.println(“输入学生学号:”);
id = sc.next();
System.out.println(“输入学生性别:”);
sex = sc.next();
if(!addStu(id,name,sex)){
System.out.println(“添加成功!”);
}else{
System.out.println(“添加失败!”);
}
break;
case 2:
System.out.println(“输入要查询学生的学号”);
id = sc.next();
selectStu(id);
break;
case 3:
System.out.println(“输入要修改的学生学号:”);
id = sc.next();
updateStu(id);
break;
case 4:
System.out.println(“输入要删除学生学号:”);
id = sc.next();
deleteStu(id);
break;
case 5:
outputStu();
break;
case 6:
System.out.println(“退出系统!”);
f = false;
break;
}
}
}
}

你可能感兴趣的:(JDBC)