DbUtil和properties工具类

DbUtil:

package querygoods.util;

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

//alt+enter提示错误
public class DBUtil {
    private Connection connection = null;
    private PreparedStatement preparedStatement = null;
    private ResultSet set = null;
    private DBUtil dbUtil = null;
    /**
     * 建立连接的方法
     *
     * @return Connection 连接对象
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    //这里之所以要用private私有化方法,是因为为了安全性,只有本类的对象或者方法里面才能调用该私有方法
    //不然挨着挨着调用的话,就体现不出封装的好处,代码复用,因为在预编译命令对象的时候调用了该私有方法
    //那么调用预编译命令方法的时候就相当于使用了该连接方法
    private Connection getConnection() throws ClassNotFoundException, SQLException {
        //在获取连接的时候,先判断连接是否为空,或者是否关闭
        if (connection == null || connection.isClosed()) {
            //通过属性集工具类调用getProp()方法获取属性的值
            String url = PropertiesUtil.getProp("url");
            String user = PropertiesUtil.getProp("user");
            String password = PropertiesUtil.getProp("password");
            String Driver = PropertiesUtil.getProp("Driver");
            Class.forName(Driver);
            connection = DriverManager.getConnection(url, user, password);
        }
        return connection;
    }

    /**
     * 获取预编译语句对象的方法
     *
     * @param sql 预编译的SQL命令
     * @return PreparedStatement对象
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    //这个方法里面个getconnection()不能使用connection,因为如果使用connection的话,那么以后用的使用需要调用上述
    //的连接方法,但是上述的连接方法是私有的,其他类无法访问,所以不能获取connection 对象,而且如果使用connection的话
    //每次连接都要用getconnection(),那么getconnection()就失去了它本身的作用.
    public PreparedStatement getPreparedStatement(String sql) throws ClassNotFoundException, SQLException {
        preparedStatement = getConnection().prepareStatement(sql);
        return preparedStatement;
    }
    /**
     * 执行增删改的方法
     *
     * @return 返回影响的行数
     * @throws SQLException
     */
    public int execUpdate(PreparedStatement ps) throws SQLException {
        return ps.executeUpdate();
    }
    /**
     * 执行查询的方法
     *
     * @param prepStat 执行查询语句的对象
     * @return 返回相应的结果集
     * @throws SQLException
     */
    public ResultSet execQuery(PreparedStatement prepStat) throws SQLException {
        return prepStat.executeQuery();
    }

    /**
     * 关闭结果集对象
     */
    public void closeSet() throws SQLException {
        if (set != null) {
            set.close();
        }
    }

    /**
     * 关闭预编译对象
     */
    public void closePrepStat() throws SQLException {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
    }

    /**
     * 关闭连接对象
     */
    public void closeConn() throws SQLException {
        if (connection != null) {
            connection.close();
        }
    }

    /**
     * 关闭全部对象
     */
    //在这个方法中可以直接调用上面的关闭方法
    public void closeAll() throws SQLException {
        closeSet();
        closePrepStat();
        closeConn();
    }
}

properties:获取配置文件里面的信息

package querygoods.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
    //定义一个属性集对象
    //定义一个字节输入流:用来读取文件中配置
    private static Properties properties = null;
    private static InputStream is = null;
    static {
        //通过本类调用class.调用类获取一个类加载器,通过类加载器调用getResourceAsStream()方法读取文件返回输入流
        is = PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
        //System.out.println(is);
        try {
            properties = new Properties();
            //然后通过属性集对象调用load方法参数是上面返回的字节输入流
            //load方法底层是把获取的属性值用一个数组存了起来
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //判断一下是否为空,如果为空而且没判断的话就容易出先空异常
                if (is != null) {
                    is.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //定义一个获取属性值得方法,这个参数就是key,返回通过属性集.getProperty(key)
    public static String getProp(String key) {
        return properties.getProperty(key);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值