jdbc工具类

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Description TODO
 * Author wanghan
 * Date 2020/12/21 16:32
 **/
public class DBUtil {
    public static String driver;
    public static String username;
    public static String password;
    public static String url;
    static {
        //准备键值对
        Properties properties = new Properties();
        //获取文件
        InputStream is = DBUtil.class.getResourceAsStream("/jdbc.properties");
        //加载流文件进入propertis
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取key value
        driver = properties.getProperty("jdbc.driver");
        url = properties.getProperty("jdbc.url");
        username = properties.getProperty("jdbc.username");
        password = properties.getProperty("jdbc.password");

        //加载驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * @ Description //获取Connection
     * @ Author wanghan
     * @ Date 17:04 2020/12/21
     */
    public static Connection getConnection(){
        Connection conn = null;
        //创建数据链接
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

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

    public static int excuteUpdate(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int n = 0;
        try {
            //获取连接
            conn = DBUtil.getConnection();
            //获取执行者
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            n = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭数据源
            DBUtil.closeAll(null,pstmt,conn);
        }
        //返回数据
        return n;
    }

    public static <T>List<T> query(Class<T> clazz,String sql,Object...args) {
        ArrayList<T> list = new ArrayList<>();
        //连接
        Connection conn = null;
        //准备preparedstatement
        PreparedStatement pstm = null;
        //返回集
        ResultSet rs = null;
        try {
            conn = getConnection();
            pstm = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                pstm.setObject(i+1,args[i]);
            }
            rs = pstm.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columncount = rsmd.getColumnCount();
            //处理结果
            while (rs.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columncount; i++) {
                    //获取列名,即对象属性
                    String columnlabel = rsmd.getColumnLabel(i + 1);
                    //根据列名获取反射对象的属性
                    Field field = clazz.getDeclaredField(columnlabel);
                    field.setAccessible(true);//属性可见
                    //本质是设置true时候,反射对象调用时候禁止Java语言访问检查(public和private)       
                    //赋值
                    field.set(t,rs.getObject(i+1));
                }
                list.add(t);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs,pstm,conn);
        }
        return list;

    }
    public static void main(String[] args) {
        Connection conn = getConnection();
        System.out.println(conn);

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值