完成了主窗口和增删改查

This commit is contained in:
2025-12-14 21:11:59 +08:00
parent a1812d346a
commit 88469c867b
11 changed files with 753 additions and 3 deletions

View File

@@ -0,0 +1,156 @@
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> wordPoList = 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();
wordPoList=commonResultSet(rs);
rs.close();
stmt.close();
}catch(SQLException e){
System.out.println("数据库异常:"+ e.getMessage());
}finally {
DBConnectionPool.releaseConnection(conn);
}
return wordPoList;
}
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());
stmt.setInt(4, question.getFrequency());
row = stmt.executeUpdate();
stmt.close();
}catch(SQLException e){
System.out.println("数据库异常:"+ e.getMessage());
}finally {
DBConnectionPool.releaseConnection(conn);
}
return row > 1;
}
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 > 1;
}
public boolean updata(Questions question){
int row = 0;
//1. 从连接池中拿到连接
Connection conn = DBConnectionPool.getConnection();
if(conn==null)
return false;
//2. 创建Statement对象
String sql = "updata 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());
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 > 1;
}
// 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"));
// }
}