JDBC封装的几个工具类

1.驱动注册,资源关闭。



import java.io.InputStream;
import java.sql.*;
import java.util.Properties;



public class myjdbcutils {

    public static 	Connection getConnection()  {
        //1.读取配置文件的信息
        InputStream inputStream=ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties= new Properties();
        properties.load(inputStream);
        String user=properties.getProperty("user");
        String password=properties.getProperty("password");
        String url=	properties.getProperty("url");
        String diverClass=	properties.getProperty("diverClass");
        //2.加在驱动
        Class.forName(diverClass);
        //3.获取连接
        Connection connection= DriverManager.getConnection(url, user, password);
        return connection;
    }
    //资源关闭的操作
    public static  void CloseResource(Connection connection,PreparedStatement ps) {
        try {
            if (ps!=null) {
                ps.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            if (connection!=null) {
                connection.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static  void CloseResource(Connection connection,PreparedStatement ps,ResultSet rs) {
        try {
            if (ps!=null) {
                ps.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            if (connection!=null) {
                connection.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            if (rs!=null)
                rs.close();


        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

2.数据增删改

package JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class AddDeleteUpdatUtils {
    public static void AddDeleteUpdat(String sql,Object ...args)  {
        //sql中占位符应该和形参一致
        Connection conn=null;
        //预编译sql
        PreparedStatement ps=null;
        try {
            conn = myjdbcutils.getConnection();
            ps = conn.prepareStatement(sql);
            //填充占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1, args[i]);
            }
            //执行
            ps.execute();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //关闭资源
            myjdbcutils.CloseResource(conn, ps);
        }
    }
}

3.查询一条数据,全部数据

package JdbcUtils;

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 SelectOneOrAllUtils {
    //得到一条
    public static  <T>  Object  getInstance(Class<T>clazz ,String sql,Object...args) {
        Connection connection=null;
        PreparedStatement pStatement=null;
        ResultSet resultSet=null;
        try {
            connection = myjdbcutils.getConnection();
            pStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                pStatement.setObject(i+1, args[i]);
            }
            resultSet = pStatement.executeQuery();
            //获取结果集
            ResultSetMetaData rsmd = resultSet.getMetaData();
            int columnCount = rsmd.getColumnCount();
            if (resultSet.next()) {
                //封装到类中
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取列值
                    Object columnvalue = resultSet.getObject(i+1);
                    //获取列的列名
                    String columnName = rsmd.getColumnLabel(i+1);
                    //给test_jdbc赋值为value,通过反射
                    Field field = clazz.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(t, columnvalue);
                }
                return t;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            myjdbcutils.CloseResource(connection, pStatement, resultSet);
        }

        return null;
    }


    //得到全部
    //---------------查询多个对象-----------
    public static <T> List<T> Selectalldata(Class<T>clazz , String sql, Object...args){
        Connection connection=null;
        PreparedStatement pStatement=null;
        ResultSet resultSet=null;
        try {
            connection = myjdbcutils.getConnection();
            pStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                pStatement.setObject(i+1, args[i]);
            }
            resultSet = pStatement.executeQuery();
            //获取结果集
            ResultSetMetaData rsmd = resultSet.getMetaData();
            int columnCount = rsmd.getColumnCount();

            //创建集合接数据
            ArrayList<T> list = new ArrayList<T>();

            while (resultSet.next()) {
                //封装到类中
                T t = clazz.newInstance();
                //处理结果集一行数据中的每一个列,给t对象指定属性赋值
                for (int i = 0; i < columnCount; i++) {
                    //获取列值
                    Object columnvalue = resultSet.getObject(i+1);
                    //获取列的列名
                    String columnName = rsmd.getColumnLabel(i+1);
                    //给test_jdbc赋值为value,通过反射
                    Field field = clazz.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(t, columnvalue);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            myjdbcutils.CloseResource(connection, pStatement, resultSet);
        }

        return null;


    }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值