当前位置: 首页 > 工具软件 > Bean Query > 使用案例 >

JAVA Bean 查询

龙星辰
2023-12-01

第一条结果查询

public class BeanQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select * from t1";
        Connection connection = JdbcUtil.getCconnection();
        Object[] parms={};
        Student student = qr.query(connection, sql,new BeanHandler<Student>(Student.class),parms);
        System.out.println(student);
        connection.close();
    }
}
package com.pp.bean;

public class Student {
    private int id;
    private String name;
    private Float ywen;
    private Float shux;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getYwen() {
        return ywen;
    }

    public void setYwen(Float ywen) {
        this.ywen = ywen;
    }

    public Float getShux() {
        return shux;
    }

    public void setShux(Float shux) {
        this.shux = shux;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", ywen='" + ywen + '\'' +
                ", shux='" + shux + '\'' +
                '}';
    }
}

多行结果返回

public class BeanQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select * from t1";
        Connection connection = JdbcUtil.getCconnection();
        Object[] parms={};
        List<Student> student = qr.query(connection, sql,new BeanListHandler<Student>(Student.class),parms);
        System.out.println(student);
        connection.close();
    }
}

获取某个列的返回集合

public class BeanQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select name from t1";
        Connection connection = JdbcUtil.getCconnection();
        Object[] parms={};
        List<Object> query = qr.query(connection, sql,new ColumnListHandler("name"),parms);
        for(Object object:query){
            System.out.println(object);
        }
        connection.close();
    }
}

单值查询

public class BeanQuery {
    public static void main(String[] args) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql="select max(ywen) from t1";
        Connection connection = JdbcUtil.getCconnection();
        Object[] parms={};
        Object query = qr.query(connection, sql,new ScalarHandler(),parms);
        System.out.println(query);
        connection.close();
    }
}

 类似资料: