1.请编程实现基于数据库的学生信息管理程序,程序的功能有:显示所有学生、新增学生、删除学生、修改学生、查找学生(根据学号、姓名、班级、性别、专业、学院等),程序采用命令行方式。
2.请编程实现把从数据库中查询出的学生信息记录集(ResultSet)中的记录转换为学生对象。
下载jar包,官网链接为:链接: 官网链接
本处只写了一种方法,其实还有一种在项目环境中的配置方法,本处不多赘述。
工具类的主要作用就是进行连接,在学生管理系统中,有多次连接和释放的过程,每次都写大段代码,阅读效果不好。将url、username、password全放入一个地方,并在该类中进行连接和释放的过程,易于代码的阅读和使用。
代码如下:
public class JDBC {
private final String url = "jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8&useSSL=true";
private final String username="root";
private final String password="root";
public JDBC() {
}
public Connection getConnection() throws SQLException, ClassNotFoundException {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接成功,数据库对象 Connection
return DriverManager.getConnection(url,username,password);
}
// 释放连接
public void closeStatement(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
resultSet.close();
statement.close();
connection.close();
}
public void close(PreparedStatement statement,Connection connection) throws SQLException {
statement.close();
connection.close();
}
}
public interface StudentSystemDao {
public void findList() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException;
public void find(String attribute, String content) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException;
public void save(Student student) throws SQLException, ClassNotFoundException;
public void delete(String id) throws SQLException, ClassNotFoundException;
public void update(String id,Student student) throws SQLException, ClassNotFoundException;
}
这个没啥好说的。
public class StudentSystem implements StudentSystemDao {
public static JDBC jdbc = new JDBC();
public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
System.out.println("欢迎使用本系统,请输入数字进行对应的操作");
int flag;
do {
System.out.println("1:查询所有学生信息 2:查询指定学生信息 3:删除学生信息 4:保存学生信息 5:修改学生信息 6:退出");
StudentSystem studentSystem = new StudentSystem();
while (true) {
try {
Scanner in = new Scanner(System.in);
flag = in.nextInt();
if (!(flag <= 6 && flag >= 1)) {
throw new Exception(new Exception());
}
break;
} catch (Exception e) {
System.out.println("输入错误,请重新输入");
}
}
Scanner in = new Scanner(System.in);
switch (flag) {
case 1: {
studentSystem.findList();
break;
}
case 2: {
System.out.println("想利用什么属性查询学生信息?");
System.out.println("1:姓名 2:年龄 3:专业 4:班级 5:学号");
int flagS;
//判断输入是否错误
while (true) {
try {
Scanner inner = new Scanner(System.in);
flagS = inner.nextInt();
if (!(flagS <= 5 && flagS >= 1)) {
throw new Exception(new Exception());
}
break;
} catch (Exception e) {
System.out.println("输入错误,请重新输入");
}
}
//通过输入的信息进行查询
String attribute = null;
switch (flagS) {
case 1: {
attribute = "name";
break;
}
case 2: {
attribute = "age";
break;
}
case 3: {
attribute = "specialized";
break;
}
case 4: {
attribute = "clazz";
break;
}
case 5: {
attribute = "id";
break;
}
}
System.out.println("请输入查询的信息:");
String content = in.next();
studentSystem.find(attribute, content);
break;
}
case 3: {
System.out.println("删除需要提供学生的id信息,请输入:");
String id = in.next();
studentSystem.delete(id);
break;
}
case 4: {
String name;
String age;
String specialized;
String clazz;
String id;
System.out.println("请输入你想保存的学生的信息");
System.out.println("姓名:");
name = in.next();
System.out.println("年龄:");
age = in.next();
System.out.println("专业");
specialized = in.next();
System.out.println("班级");
clazz = in.next();
System.out.println("学号");
id = in.next();
Student student = new Student(name, age, specialized, clazz, id);
studentSystem.save(student);
break;
}
case 5: {
System.out.println("修改学生信息需要提供学号,请输入:");
String id = in.next();
String name;
String age;
String specialized;
String clazz;
System.out.println("请输入你想保存的学生的信息");
System.out.println("姓名:");
name = in.next();
System.out.println("年龄:");
age = in.next();
System.out.println("专业");
specialized = in.next();
System.out.println("班级");
clazz = in.next();
System.out.println("学号");
id = in.next();
Student student = new Student(name, age, specialized, clazz, id);
studentSystem.update(id, student);
System.out.println("修改学生信息成功");
break;
}
}
} while (flag != 6);
}
@Override
public void findList() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
Connection connection = jdbc.getConnection();
String sql = "select * from student";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
// while (resultSet.next()){
// System.out.println("姓名:" + resultSet.getString("name")
// + " 年龄:" + resultSet.getString("age")
// +" 专业:"+ resultSet.getString("specialized")
// +" 班级:" + resultSet.getString("clazz")
// +" 学号:" + resultSet.getString("id")
// );
// }
List<Student> list = populate(resultSet);
for (Student s : list) {
System.out.println("姓名:" + s.name
+ " 年龄:" + s.age
+ " 专业:" + s.specialized
+ " 班级:" + s.clazz
+ " 学号:" + s.id
);
}
jdbc.closeStatement(resultSet, statement, connection);
}
@Override
public void find(String attribute, String content) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
Connection connection = jdbc.getConnection();
String sql = "select * from student where " + attribute + "= " + "\"" + content + "\"";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
if (resultSet == null) {
System.out.println("输入的" + attribute + "在数据库中无对应数据,请重新输入");
} else {
// while (resultSet.next()){
// System.out.println("姓名:" + resultSet.getString("name")
// + " 年龄:" + resultSet.getString("age")
// +" 专业:"+ resultSet.getString("specialized")
// +" 班级:" + resultSet.getString("clazz")
// +" 学号:" + resultSet.getString("id")
// );
// }
List<Student> list = populate(resultSet);
for (Student s : list) {
System.out.println("姓名:" + s.name
+ " 年龄:" + s.age
+ " 专业:" + s.specialized
+ " 班级:" + s.clazz
+ " 学号:" + s.id
);
}
}
jdbc.closeStatement(resultSet, statement, connection);
}
@Override
public void save(Student student) throws SQLException, ClassNotFoundException {
Connection connection = jdbc.getConnection();
String sql = "insert into student(name,age,specialized,clazz,id) values (?,?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, student.name);
ps.setString(2, String.valueOf(student.age));
ps.setString(3, student.specialized);
ps.setString(4, student.clazz);
ps.setString(5, student.id);
int i = ps.executeUpdate();
if (i != 0) {
System.out.println("保存数据成功");
} else {
System.out.println("保存数据失败");
}
jdbc.close(ps, connection);
}
@Override
public void delete(String id) throws SQLException, ClassNotFoundException {
Connection connection = jdbc.getConnection();
String sql = "DELETE FROM student WHERE id = " + id;
PreparedStatement ps = connection.prepareStatement(sql);
int i = ps.executeUpdate();
if (i != 0) {
System.out.println("删除数据成功");
} else {
System.out.println("删除数据失败");
}
jdbc.close(ps, connection);
}
@Override
public void update(String id, Student student) throws SQLException, ClassNotFoundException {
Connection connection = jdbc.getConnection();
String sql = "update student set " + "name = \'" + student.name + "\' ,age = \'" + student.age
+ "\' ,specialized= \'" + student.specialized + "\' ,clazz= \'" + student.clazz
+ "\' WHERE id = \'" + id + "\'";
System.out.println(sql);
PreparedStatement ps = connection.prepareStatement(sql);
int i = ps.executeUpdate();
if (i != 0) {
System.out.println("更新数据成功");
} else {
System.out.println("更新数据失败");
}
jdbc.close(ps, connection);
}
public List populate(ResultSet rs) throws SQLException, InstantiationException, IllegalAccessException {
List<Student> studentList = new ArrayList<>();
while (rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
String specialized = rs.getString("specialized");
String clazz = rs.getString("clazz");
String id = rs.getString("id");
Student student = new Student(name , age ,specialized ,clazz ,id);
studentList.add(student);
}
return studentList;
}
}
文章到这里就结束了,感谢大家的观看,如果有哪里写的不对,欢迎大家指正。