用java代码+MySQL代码实现上述问题
时间: 2023-12-18 17:05:41 浏览: 64
java连接MySQL数据库实现代码
好的,下面是一个简单的Java代码实现学生选课系统的示例,使用了Java JDBC连接MySQL数据库:
1. 首先需要创建数据库和数据表,可以使用以下SQL语句:
```
CREATE DATABASE `course_selection` CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `course_selection`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`gender` varchar(10) NOT NULL,
`age` int(11) NOT NULL,
`major` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`teacher` varchar(50) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`gender` varchar(10) NOT NULL,
`age` int(11) NOT NULL,
`course` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `course_selection` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `student_id` (`student_id`),
KEY `course_id` (`course_id`),
CONSTRAINT `course_selection_ibfk_1` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `course_selection_ibfk_2` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
2. 然后创建Java项目,引入MySQL连接驱动jar包,可以使用以下代码连接MySQL数据库:
```
public class DatabaseUtil {
private static final String URL = "jdbc:mysql://localhost:3306/course_selection?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
private static Connection conn = null;
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
return conn;
}
}
```
3. 接下来就可以实现各个功能模块的Java代码了,例如:
学生信息管理:
```
public class StudentDao {
public void add(Student student) {
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
try {
String sql = "INSERT INTO student(name, gender, age, major) VALUES (?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, student.getName());
ps.setString(2, student.getGender());
ps.setInt(3, student.getAge());
ps.setString(4, student.getMajor());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void delete(int id) {
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
try {
String sql = "DELETE FROM student WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void update(Student student) {
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
try {
String sql = "UPDATE student SET name=?, gender=?, age=?, major=? WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, student.getName());
ps.setString(2, student.getGender());
ps.setInt(3, student.getAge());
ps.setString(4, student.getMajor());
ps.setInt(5, student.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<Student> queryAll() {
List<Student> list = new ArrayList<>();
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT * FROM student";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setMajor(rs.getString("major"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
```
选课管理:
```
public class CourseSelectionDao {
public void add(CourseSelection courseSelection) {
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
try {
String sql = "INSERT INTO course_selection(student_id, course_id) VALUES (?, ?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, courseSelection.getStudentId());
ps.setInt(2, courseSelection.getCourseId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void delete(int id) {
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
try {
String sql = "DELETE FROM course_selection WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<CourseSelection> queryAll() {
List<CourseSelection> list = new ArrayList<>();
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT * FROM course_selection";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
CourseSelection courseSelection = new CourseSelection();
courseSelection.setId(rs.getInt("id"));
courseSelection.setStudentId(rs.getInt("student_id"));
courseSelection.setCourseId(rs.getInt("course_id"));
list.add(courseSelection);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
```
成绩管理:
```
public class ScoreDao {
public void add(Score score) {
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
try {
String sql = "INSERT INTO score(student_id, course_id, score) VALUES (?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, score.getStudentId());
ps.setInt(2, score.getCourseId());
ps.setInt(3, score.getScore());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void update(Score score) {
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
try {
String sql = "UPDATE score SET score=? WHERE student_id=? AND course_id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, score.getScore());
ps.setInt(2, score.getStudentId());
ps.setInt(3, score.getCourseId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<Score> queryAll() {
List<Score> list = new ArrayList<>();
Connection conn = DatabaseUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT * FROM score";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Score score = new Score();
score.setId(rs.getInt("id"));
score.setStudentId(rs.getInt("student_id"));
score.setCourseId(rs.getInt("course_id"));
score.setScore(rs.getInt("score"));
list.add(score);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close
阅读全文