后端开发PreparedStatement实现表的通用查询

Posted cls1277

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了后端开发PreparedStatement实现表的通用查询相关的知识,希望对你有一定的参考价值。

PreparedStatement实现表的通用查询

实现了通用的JDBC查找功能,包括查询结果会有很多行的情况

package com.cls1277.commonquery;

import com.cls1277.preparedstatement.customer.Customer;
import com.cls1277.utils.JDBCutils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

public class CommonQueryTest 
    //泛型方法
    public <T> T getCommonInstance(Class<T> clazz, String sql, Object ... args) 
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try 
            conn = JDBCutils.getConnection();
            ps = conn.prepareStatement(sql);
            for(int i=0; i<args.length; i++) 
                ps.setObject(i+1, args[i]);
            
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            if(rs.next()) 
                T t = clazz.newInstance();
                for(int i=0; i<columnCount; i++) 
                    Object columnValue = rs.getObject(i+1);
                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                
                return t;
            
         catch (Exception e) 
            e.printStackTrace();
         finally 
            JDBCutils.closeResource(conn, ps, rs);
        
        return null;
    

    @Test
    public void getInstanceTest() 
        String sql = "select id,name,email from customers where id=?";
        Customer customer = getCommonInstance(Customer.class, sql, 12);
        System.out.println(customer);
    

    public <T> List<T> getInstance(Class<T> clazz, String sql, Object ... args) 
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try 
            conn = JDBCutils.getConnection();
            ps = conn.prepareStatement(sql);
            for(int i=0; i<args.length; i++) 
                ps.setObject(i+1, args[i]);
            
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            ArrayList<T> ts = new ArrayList<>();
            while(rs.next()) 
                T t = clazz.newInstance();
                for(int i=0; i<columnCount; i++) 
                    Object columnValue = rs.getObject(i+1);
                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                
                ts.add(t);
            
            return ts;
         catch (Exception e) 
            e.printStackTrace();
         finally 
            JDBCutils.closeResource(conn, ps, rs);
        
        return null;
    

    @Test
    public void getInstanceListTest() 
        String sql = "select id,name,email,birth from customers where id<?";
        List<Customer> customers = getInstance(Customer.class, sql, 12);
        for(Customer customer:customers) 
            System.out.println(customer);
        
    


以上是关于后端开发PreparedStatement实现表的通用查询的主要内容,如果未能解决你的问题,请参考以下文章

后端开发PreparedStatement实现表的增删改查

后端开发PreparedStatement实现表的增删改查

后端开发PreparedStatement的优点以及实现高效批量插入

后端开发PreparedStatement的优点以及实现高效批量插入

后端开发PreparedStatement的优点以及实现高效批量插入

java后台开发中字典表的设计和使用