《小型学生管理系统——Spring+MySQL》,可执行Java代码,注解方式
功能介绍:
本系统可以实现数据库中学生数据的增删改查,使用Spring+MySQL实现。
第一步:建立数据库连接
package com.yann.students;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil2019 {
public static Connection connection() {
Connection connection = null;
String userInfo = "jdbc:mysql://localhost:3306/students?useUnicode" +
"=true&characterEncoding=utf8";
String name = "root";
String password = "root";
try {
connection = DriverManager.getConnection(userInfo, name, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
这里需要牢记的是DriverManager.getConnection()这个方法,不过connection要做成全局变量,不然后面没有办法做返回值。
第二步:写接口,对学生数据表的四种操作,增删改查
package com.yann.students;
import java.util.List;
public interface IStudentMethod {
public void insert(StudentInfo studentInfo);
public void delete(int studentId);
public void update(StudentInfo studentInfo);
public List<StudentInfo> result();
}
这里需要注意的是,不一定每个方法都没有返回值,最后的查询方法,它的返回值是一个列表的形式。还有就是注意形参也不都一样,在删除这个操作上面,形参是一个数字,也就是我们可以依靠一个id号去删除掉一个学生。
第三步,实现接口中的方法
package com.yann.students;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Component
public class StudentMethodImpl implements IStudentMethod {
Connection connection = DBUtil2019.connection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
@Override
public void insert(StudentInfo studentInfo) {
String sql = "INSERT INTO student_info VALUES(null,?,?)";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, studentInfo.getStudentName());
preparedStatement.setString(2, studentInfo.getStudentGender());
preparedStatement.executeUpdate();
System.out.println("添加成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(int studentId) {
String sql = "DELETE FROM student_info WHERE id = ?";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, studentId);
preparedStatement.executeUpdate();
System.out.println("删除成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(StudentInfo studentInfo) {
String sql = "UPDATE student_info SET gender = ? WHERE name = ?";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, studentInfo.getStudentGender());
preparedStatement.setString(2, studentInfo.getStudentName());
preparedStatement.executeUpdate();
System.out.println("更新成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<StudentInfo> result() {
ArrayList<StudentInfo> studentInfos = new ArrayList<>();
String sql = "SELECT * FROM student_info";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
StudentInfo studentInfo =
new StudentInfo(resultSet.getInt(1),
resultSet.getString(2),
resultSet.getString(3));
studentInfos.add(studentInfo);
}
System.out.println("查询成功");
} catch (SQLException e) {
e.printStackTrace();
}
return studentInfos;
}
}
这个部分也是本文中代码最多的部分,可以拆分为4个部分去查看它,分别是增加方法、删除方法、更新方法和查询方法。
第四步,一顿操作,对数据库中的数据进行一些操作
package com.yann.students;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
@Component("studentMethodImpService")
public class StudentMethodImpService {
@Resource
private StudentMethodImpl studentMethod;
public StudentMethodImpl getStudentMethod() {
return studentMethod;
}
public void setStudentMethod(StudentMethodImpl studentMethod) {
this.studentMethod = studentMethod;
}
public void insert(){
studentMethod.insert(new StudentInfo("李四","男"));
studentMethod.insert(new StudentInfo("王五","男"));
studentMethod.insert(new StudentInfo("赵六","男"));
}
public void delete(){
studentMethod.delete(2);
}
public void update(){
studentMethod.update(new StudentInfo("张三","女"));
}
}
public void index() {
List<StudentInfo> result = studentMethod.result();
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
}
}
上面分别是增、删、改和查的操作,大家可以根据自己数据库表的实际情况进行一些微调。
第五步,做测试
package com.yann.students;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentMethodImpService studentMethodImpService = (StudentMethodImpService) context.getBean(
"studentMethodImpService");
studentMethodImpService.index();
}
}
因为本文基于Spring实现,所以最后直接通过注解去赋值,然后一顿操作,接下来附上xml文件的代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan
base-package="com.yann.playMusic"></context:component-scan>
<context:component-scan
base-package="com.yann.user"></context:component-scan>
<context:component-scan
base-package="com.yann.students"></context:component-scan>
</beans>
上述就是整个系统的所有代码,接下来希望自己能早一点进入前端,加上前端的页面这个系统就基本上有模有样了。