目录
在jdbc中的数据表增删改查中,查找是相对复杂一些的操作,虽然复杂但是不难。我们这里的通用指的是当我们在同一张表格进行查询时,无论是查询的字段名,查询的条件虽然不同,但是只要使用了我们写好的通用查询方法,都可以查询出来结果集,如下:
1,当我们使用stu_name作为查询的条件时,查询到的结果集为:

2,当我们使用的是stu_id作为查询的条件,且查询的字段数更加多时,结果集如下:

如上,我们可以看到,无论是字段数,字段名或者是查询条件虽然有所不同,但是依旧可以返回我们的结果集。现在我们一起去编写针对同一张数据表的查询代码:
无论是查询还是增删改操作,都或多或少的会有ORM(对象联系映射)模式的应用。使用该模式可以方便我们对数据表中的记录进行相关的操作。因此我们最好将我们创建的java类中的属性与我们数据表中的所有字段名一一对应(一个都不落):

之后再去创建公有的方法来设置及获取属性值,重写toString方法和编写有参构造方法及无参构造方法,代码如下:
- public class QueryBean {
- private int stu_id;
- private String stu_name;
- private int stu_pai;
- private double stu_score;
- private double stu_get1;
- private double stu_get2;
- private double stu_total;
- private int sch_id;
- public double getStu_get1() {
- return stu_get1;
- }
- public void setStu_get1(double stu_get1) {
- this.stu_get1 = stu_get1;
- }
- public double getStu_get2() {
- return stu_get2;
- }
- public void setStu_get2(double stu_get2) {
- this.stu_get2 = stu_get2;
- }
- public double getStu_total() {
- return stu_total;
- }
- public void setStu_total(double stu_total) {
- this.stu_total = stu_total;
- }
- public int getSch_id() {
- return sch_id;
- }
- public void setSch_id(int sch_id) {
- this.sch_id = sch_id;
- }
- public QueryBean() {}
- public int getStu_id() {
- return stu_id;
- }
- public void setStu_id(int stu_id) {
- this.stu_id = stu_id;
- }
- public String getStu_name() {
- return stu_name;
- }
- public void setStu_name(String stu_name) {
- this.stu_name = stu_name;
- }
- public int getStu_pai() {
- return stu_pai;
- }
- public void setStu_pai(int stu_pai) {
- this.stu_pai = stu_pai;
- }
- public double getStu_score() {
- return stu_score;
- }
- public void setStu_score(double stu_score) {
- this.stu_score = stu_score;
- }
-
- public QueryBean(int stu_id, String stu_name, int stu_pai, double stu_score, double stu_get1, double stu_get2,
- double stu_total, int sch_id) {
- super();
- this.stu_id = stu_id;
- this.stu_name = stu_name;
- this.stu_pai = stu_pai;
- this.stu_score = stu_score;
- this.stu_get1 = stu_get1;
- this.stu_get2 = stu_get2;
- this.stu_total = stu_total;
- this.sch_id = sch_id;
- }
- @Override
- public String toString() {
- return "QueryBean [stu_id=" + stu_id + ", stu_name=" + stu_name + ", stu_pai=" + stu_pai + ", stu_score="
- + stu_score + ", stu_get1=" + stu_get1 + ", stu_get2=" + stu_get2 + ", stu_total=" + stu_total
- + ", sch_id=" + sch_id + "]";
- }
- }
首先我们还是需要经过固有的几个步骤:
代码如下:
- public static QueryBean getQuery(String sql,Object...args) throws Exception {
- Connection conn=JdbcUtils.getConnection();
- PreparedStatement ps=conn.prepareStatement(sql);
- //填充占位符
- for(int i=0;i
- ps.setObject(i+1, args[i]);
- }
- ResultSet rs=ps.executeQuery();
- ResultSetMetaData rsmd=rs.getMetaData(); //获取到结果集的元数据
- int colunmCount=rsmd.getColumnCount();
- if(rs.next()) { //如果找到了匹配的,就执行该代码块
- QueryBean student=new QueryBean();
- for(int i=0;i
//遍历要显示的列数 - String columnName =rsmd.getColumnName(i+1);
- Object columnValue=rs.getObject(i+1);
- Field field=QueryBean.class.getDeclaredField(columnName); //使用java的反射机制
- field.setAccessible(true);
- field.set(student,columnValue);
- }
- return student; //返回我们的对象
- }
- return null; //如果没有匹配到的,统一使用该字段类型的默认初始化值进行赋值
- }
-
- }
在主方法中使用
编写好后,我们就可以在主方法中进行调用和使用:
- public class Query1 {
- public static void main(String[] args) {
- PreparedStatement ps=null;
- ResultSet rs=null;
- Connection conn=null;
- try {
- conn=JdbcUtils.getConnection(); //自定义的通用数据库
- String sql="SELECT stu_id,stu_name,stu_pai,stu_score FROM 20student WHERE stu_id=?";
- QueryBean student=JdbcUtils.getQuery(sql,1); //调用我们的查询方法
- System.out.println(student); //打印我们的对象
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- JdbcUtils.CloseResource(conn, ps,rs); //关闭资源
- }
- }
-
- }
如上我们只需要编写三个java文件即可实现简单的数据库查询操作,如果有问题的请在评论区留言。