冲刺第六天
昨天任务是完成多条件的查询
今天的任务依旧是完成多条件的查询
困难时如何将查询结果展示在前端界面
package com.example.tuandui.dao;
import com.example.tuandui.entity.Jz;
import com.example.tuandui.utils.DbOpenHelper;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.sql.PreparedStatement;
import java.util.List;
public class Jzdao extends DbOpenHelper {
public static List<Jz> queryStudents(String donator, String location, String classes) {
List<Jz> jzs = new ArrayList<>();
try {
DbOpenHelper.getConnection(); // 获取数据库连接
String sql = "SELECT * FROM jz_table WHERE donator LIKE CONCAT('%',?, '%') AND location LIKE CONCAT('%',?, '%') AND classes LIKE CONCAT('%',?, '%')";
//使用PreparedStatement来执行SQL查询
PreparedStatement pstmt = DbOpenHelper.conn.prepareStatement(sql);
pstmt.setString(1,donator);
pstmt.setString(2, location);
pstmt.setString(3, classes);
//遍历ResultSet每获取一行数据就创建一个Jz对象,并将相应字段的值赋给该对象,最后将这个对象添加到结果列表jzs中。
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Jz jz = new Jz();
jz.setDonator(rs.getString("donator"));
jz.setLocation(rs.getString("location"));
jz.setClasses(rs.getString("classes"));
jzs.add(jz);
}
DbOpenHelper.closeAll(); // 关闭所有数据库操作对象
} catch (Exception ex) {
ex.printStackTrace();
}
return jzs;
}
}
import com.example.tuandui.entity.Jz;
import com.example.tuandui.utils.DbOpenHelper;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.sql.PreparedStatement;
import java.util.List;
public class Jzdao extends DbOpenHelper {
public static List<Jz> queryStudents(String donator, String location, String classes) {
List<Jz> jzs = new ArrayList<>();
try {
DbOpenHelper.getConnection(); // 获取数据库连接
String sql = "SELECT * FROM jz_table WHERE donator LIKE CONCAT('%',?, '%') AND location LIKE CONCAT('%',?, '%') AND classes LIKE CONCAT('%',?, '%')";
//使用PreparedStatement来执行SQL查询
PreparedStatement pstmt = DbOpenHelper.conn.prepareStatement(sql);
pstmt.setString(1,donator);
pstmt.setString(2, location);
pstmt.setString(3, classes);
//遍历ResultSet每获取一行数据就创建一个Jz对象,并将相应字段的值赋给该对象,最后将这个对象添加到结果列表jzs中。
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Jz jz = new Jz();
jz.setDonator(rs.getString("donator"));
jz.setLocation(rs.getString("location"));
jz.setClasses(rs.getString("classes"));
jzs.add(jz);
}
DbOpenHelper.closeAll(); // 关闭所有数据库操作对象
} catch (Exception ex) {
ex.printStackTrace();
}
return jzs;
}
}