四、在 eclipse 平台中,通过 jdbc 建立动态网站到数据库的连接类 DBConnect; 并建立 StudentDAO 完成应用程序对数据库的增删改查操作;并建立 CourseDAO 完成应用程序对数据库的增删改查操作;建立 ScoreDAO 完成应用程序对数据库 的增删改查操作。
时间: 2024-04-22 16:21:38 浏览: 131
好的,根据您的要求,我来为您编写建立动态网站到数据库的连接类DBConnect、StudentDAO、CourseDAO和ScoreDAO的Java代码:
1. DBConnect类:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnect {
private static final String URL = "jdbc:mysql://localhost:3306/stuDB?useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "123456";
// 获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
```
2. StudentDAO类:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// 查询所有学生信息
public List<Student> findAll() {
List<Student> list = new ArrayList<Student>();
try {
conn = DBConnect.getConnection();
String sql = "SELECT * FROM StuInfo";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Student stu = new Student();
stu.setXhao(rs.getInt("xhao"));
stu.setXming(rs.getString("xming"));
stu.setXbie(rs.getString("xbie"));
stu.setBji(rs.getString("bji"));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return list;
}
// 根据学号查询学生信息
public Student findByXhao(int xhao) {
Student stu = null;
try {
conn = DBConnect.getConnection();
String sql = "SELECT * FROM StuInfo WHERE xhao=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, xhao);
rs = pstmt.executeQuery();
if (rs.next()) {
stu = new Student();
stu.setXhao(rs.getInt("xhao"));
stu.setXming(rs.getString("xming"));
stu.setXbie(rs.getString("xbie"));
stu.setBji(rs.getString("bji"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return stu;
}
// 添加学生信息
public boolean add(Student stu) {
boolean flag = false;
try {
conn = DBConnect.getConnection();
String sql = "INSERT INTO StuInfo(xhao,xming,xbie,bji) VALUES(?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, stu.getXhao());
pstmt.setString(2, stu.getXming());
pstmt.setString(3, stu.getXbie());
pstmt.setString(4, stu.getBji());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return flag;
}
// 修改学生信息
public boolean update(Student stu) {
boolean flag = false;
try {
conn = DBConnect.getConnection();
String sql = "UPDATE StuInfo SET xming=?,xbie=?,bji=? WHERE xhao=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stu.getXming());
pstmt.setString(2, stu.getXbie());
pstmt.setString(3, stu.getBji());
pstmt.setInt(4, stu.getXhao());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return flag;
}
// 删除学生信息
public boolean delete(int xhao) {
boolean flag = false;
try {
conn = DBConnect.getConnection();
String sql = "DELETE FROM StuInfo WHERE xhao=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, xhao);
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return flag;
}
// 关闭连接
private void closeAll() {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. CourseDAO类:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CourseDAO {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// 查询所有课程信息
public List<Course> findAll() {
List<Course> list = new ArrayList<Course>();
try {
conn = DBConnect.getConnection();
String sql = "SELECT * FROM CourseInfo";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Course course = new Course();
course.setKcbma(rs.getString("kcbma"));
course.setKcming(rs.getString("kcming"));
course.setXfen(rs.getInt("xfen"));
list.add(course);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return list;
}
// 根据课程编号查询课程信息
public Course findByKcbma(String kcbma) {
Course course = null;
try {
conn = DBConnect.getConnection();
String sql = "SELECT * FROM CourseInfo WHERE kcbma=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, kcbma);
rs = pstmt.executeQuery();
if (rs.next()) {
course = new Course();
course.setKcbma(rs.getString("kcbma"));
course.setKcming(rs.getString("kcming"));
course.setXfen(rs.getInt("xfen"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return course;
}
// 添加课程信息
public boolean add(Course course) {
boolean flag = false;
try {
conn = DBConnect.getConnection();
String sql = "INSERT INTO CourseInfo(kcbma,kcming,xfen) VALUES(?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, course.getKcbma());
pstmt.setString(2, course.getKcming());
pstmt.setInt(3, course.getXfen());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return flag;
}
// 修改课程信息
public boolean update(Course course) {
boolean flag = false;
try {
conn = DBConnect.getConnection();
String sql = "UPDATE CourseInfo SET kcming=?,xfen=? WHERE kcbma=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, course.getKcming());
pstmt.setInt(2, course.getXfen());
pstmt.setString(3, course.getKcbma());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return flag;
}
// 删除课程信息
public boolean delete(String kcbma) {
boolean flag = false;
try {
conn = DBConnect.getConnection();
String sql = "DELETE FROM CourseInfo WHERE kcbma=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, kcbma);
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return flag;
}
// 关闭连接
private void closeAll() {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
4. ScoreDAO类:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ScoreDAO {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// 查询所有成绩信息
public List<Score> findAll() {
List<Score> list = new ArrayList<Score>();
try {
conn = DBConnect.getConnection();
String sql = "SELECT * FROM ScoreInfo";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Score score = new Score();
score.setXhao(rs.getInt("xhao"));
score.setKcbma(rs.getString("kcbma"));
score.setXqi(rs.getInt("xqi"));
score.setCji(rs.getInt("cji"));
list.add(score);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return list;
}
// 根据学号和课程编号查询成绩信息
public Score findByXhaoAndKcbma(int xhao, String kcbma) {
Score score = null;
try {
conn = DBConnect.getConnection();
String sql = "SELECT * FROM ScoreInfo WHERE xhao=? AND kcbma=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, xhao);
pstmt.setString(2, kcbma);
rs = pstmt.executeQuery();
if (rs.next()) {
score = new Score();
score.setXhao(rs.getInt("xhao"));
score.setKcbma(rs.getString("kcbma"));
score.setXqi(rs.getInt("xqi"));
score.setCji(rs.getInt("cji"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return score;
}
// 添加成绩信息
public boolean add(Score score) {
boolean flag = false;
try {
conn = DBConnect.getConnection();
String sql = "INSERT INTO ScoreInfo(xhao,kcbma,xqi,cji) VALUES(?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, score.getXhao());
pstmt.setString(2, score.getKcbma());
pstmt.setInt(3, score.getXqi());
pstmt.setInt(4, score.getCji());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return flag;
}
// 修改成绩信息
public boolean update(Score score) {
boolean flag = false;
try {
conn = DBConnect.getConnection();
String sql = "UPDATE ScoreInfo SET xqi=?,cji=? WHERE xhao=? AND kcbma=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, score.getXqi());
pstmt.setInt(2, score.getCji());
pstmt.setInt(3, score.getXhao());
pstmt.setString(4, score.getKcbma());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return flag;
}
//
阅读全文