Files
DataManagerSystem/src/backend/dao/impl/QuestionsDao.java
2025-12-28 21:58:11 +08:00

163 lines
5.8 KiB
Java

package backend.dao.impl;
import backend.model.Questions;
import backend.utils.DBConnectionPool;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class QuestionsDao {
List<Questions> commonResultSet(ResultSet rs) throws SQLException{
List<Questions> questionPoList = new ArrayList<>();
while(rs.next()){
Questions questions = new Questions();
questions.setId(rs.getInt("id"));
questions.setName(rs.getString("name"));
questions.setDescription(rs.getString("description"));
questions.setAnswer(rs.getString("answer"));
questions.setFrequency(rs.getInt("frequency"));
questionPoList.add(questions);
}
return questionPoList;
}
public List<Questions> getAll() {
List<Questions> questionPoList = new ArrayList<>();
//1. 从连接池中拿到连接
Connection conn = DBConnectionPool.getConnection();
if(conn==null)
return null;
//2. 创建Statement对象
String sql = "select id,name,description,answer,frequency from questions";
try{
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
questionPoList=commonResultSet(rs);
rs.close();
stmt.close();
}catch(SQLException e){
System.out.println("数据库异常:"+ e.getMessage());
}finally {
DBConnectionPool.releaseConnection(conn);
}
return questionPoList;
}
public List<Questions> getByCondition(Integer frequency,String key) {
List<Questions> questionPoList = null;
//1. 从连接池中拿到连接
Connection conn = DBConnectionPool.getConnection();
if(conn==null)
return null;
//2. 创建Statement对象
String sql = "select id,name,description,answer,frequency from questions where name like ?";
if(frequency != null)
sql += " and frequency = ?";
try{
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "%"+key+"%");
if(frequency != null)
stmt.setInt(2, frequency);
ResultSet rs = stmt.executeQuery();
questionPoList =commonResultSet(rs);
rs.close();
stmt.close();
}catch(SQLException e){
System.out.println("数据库异常:"+ e.getMessage());
}finally {
DBConnectionPool.releaseConnection(conn);
}
return questionPoList;
}
public boolean insert(Questions question){
int row = 0;
//1. 从连接池中拿到连接
Connection conn = DBConnectionPool.getConnection();
if(conn==null)
return false;
//2. 创建Statement对象
String sql = "insert into questions(name,description,answer,frequency) values(?,?,?,?)";
try{
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, question.getName());
stmt.setString(2, question.getDescription());
stmt.setString(3, question.getAnswer());
if(question.getFrequency()==null)
stmt.setInt(4, 0);
else
stmt.setInt(4, question.getFrequency());
row = stmt.executeUpdate();
stmt.close();
}catch(SQLException e){
System.out.println("数据库异常:"+ e.getMessage());
}finally {
DBConnectionPool.releaseConnection(conn);
}
return row > 0;
}
public boolean delete(Integer id){
int row = 0;
//1. 从连接池中拿到连接
Connection conn = DBConnectionPool.getConnection();
if(conn==null)
return false;
//2. 创建Statement对象
String sql = "delete from questions where id = ?";
try{
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
row = stmt.executeUpdate();
stmt.close();
}catch(SQLException e){
System.out.println("数据库异常:"+ e.getMessage());
}finally {
DBConnectionPool.releaseConnection(conn);
}
return row > 0;
}
public boolean update(Questions question){
int row = 0;
//1. 从连接池中拿到连接
Connection conn = DBConnectionPool.getConnection();
if(conn==null)
return false;
//2. 创建Statement对象
String sql = "update questions set name = ?,description = ?,answer = ?,frequency=? where id = ?";
try{
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, question.getName());
stmt.setString(2, question.getDescription());
stmt.setString(3, question.getAnswer());
if(question.getFrequency()==null)
stmt.setInt(4, 0);
else
stmt.setInt(4, question.getFrequency());
stmt.setInt(5, question.getId());
row = stmt.executeUpdate();
stmt.close();
}catch(SQLException e){
System.out.println("数据库异常:"+ e.getMessage());
}finally {
DBConnectionPool.releaseConnection(conn);
}
return row > 0;
}
// public static void main(String[] args) {
// WordDao wordDao = new WordDao();
// //查询
// List<WordPo> wordPoList = wordDao.getByCondition(3,"v");
// if(wordPoList != null)
// for(WordPo wordPo:wordPoList)
// System.out.println(wordPo.getWord());
// //增加
// wordDao.insert(new WordPo("test", "test", "test", 1));
// //更新
// wordDao.updata(new WordPo(1, 1, "test", "test", "test"));
// }
}