JDBCUtil数据库查询工具类的终极封装

版权声明:四川华迪信息技术有限公司java实训班乔二 https://blog.csdn.net/qxqx451/article/details/79422029

JDBCUtil.class

package com.hwadee.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.hwadee.entity.Book;
import com.hwadee.entity.User;

public class JDBCUtil {
    public static String url = null;
    public static String username = null;
    public static String password = null;
    public static String Driver = null;
    static {
        InputStream inputStream = JDBCUtil.class.getResourceAsStream("/db.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            Driver = properties.getProperty("Driver");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //加载数据库驱动
        try {
            Class.forName(Driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
/**
 * 获取数据库连接对象
 * @return
 */
    public static Connection getConnection() {
        try {
            Connection conn = DriverManager.getConnection(url, username, password);
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

/**
 * 添加删除修改数据
 * @return
 */
    public static boolean insertDeleteUpdateExecute(String sql,ArrayList<Object> paras) {
        Connection conn=getConnection();
        PreparedStatement pst=null;
        boolean flag = false;
        int index = 1;
        try {
            pst=conn.prepareStatement(sql);
            if(paras != null&& paras.size()>0) {
                pst.clearParameters();
                for(int i = 0;i<paras.size();i++) {
                    pst.setObject(index++, paras.get(i));
                }
            }
            int result=pst.executeUpdate();
            flag = result > 0? true : false;
        } catch (SQLException e) {
            flag = false;
            e.printStackTrace();
        }finally {
            close(conn, pst);
        }
        return flag;    
    }
    /**
     * 获取单个对象 可用于登录注册验证
     * @param sql
     * @param paras
     * @param cls
     * @return
     */
    public static <T> T findBySingleObject(String sql,ArrayList<Object> paras,Class<T> cls) {
        Connection conn =getConnection();
        PreparedStatement pst = null;
        ResultSet rs=null;
        T singleObject = null;
        int index = 1;

        try {
            pst = conn.prepareStatement(sql);
            if(paras != null && paras.size()>0) {
                pst.clearParameters();   
                for(int i = 0;i<paras.size();i++) {
                    pst.setObject(index++,paras.get(i));
                }
            }
            rs = pst.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            while(rs.next()) {
                singleObject = cls.newInstance();
                for(int i = 0;i<columnCount;i++) {
                    String columnName = rsmd.getColumnName(i+1);
                    Object columnValue = rs.getObject(columnName);
                    Field field = cls.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(singleObject, columnValue);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close(conn, pst);

        }
        return singleObject;

    }
    /**
     * 
     * 列表查询
     */
    public static<T> List<T> queryListExecute(String sql,ArrayList<Object> paras,Class<T> cls ){
        Connection conn = getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        T singleObject = null;
        int index = 1;
        List <T> list = new ArrayList<T>();
        try {
            pst = conn.prepareStatement(sql);
            if(paras !=null && paras.size()>0) {
                pst.clearParameters();
                for(int i = 0;i<paras.size();i++) {
                    pst.setObject(index++, paras.get(i));
                }   
            }
            rs = pst.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            while(rs.next()) {
                singleObject = cls.newInstance();
                for(int i = 0;i<columnCount;i++) {
                    String columnName = rsmd.getColumnName(i+1);
                    Object columdValue = rs.getObject(columnName);
                    Field field = cls.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(singleObject, columdValue);
                }
                list.add(singleObject);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            close(conn, pst, rs);
        }

        return list;
    }


    /*
     * 释放资源
     */
    public static void close(Connection conn, PreparedStatement pst) {
        if (pst != null) {
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, PreparedStatement pst,
            ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (pst != null) {
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}   

db.properties

url = jdbc:mysql://localhost:3306/library?zeroDateTimeBehavior=convertToNull
username = root
password = 123456
Driver = com.mysql.jdbc.Driver

猜你喜欢

转载自blog.csdn.net/qxqx451/article/details/79422029