JavaWeb/jsp/properties文件放置与读取

将properties文件放置在src文件夹:

public class DBUtils {
    private static String url = null;
    private static String user = null;
    private static String password = null;
    private static String driverClass = null;
    private static InputStream in = null;
    
    //利用静态代码块的特征,在类文件加载到内存的时候,就会执行在静态代码块里面的代码
    static {
        try {
            //1. 读取配置文件信息 读取properties文件
            Properties props = new Properties();
            
            
            //如果一个properties文件加载到内存中,需要借助于IO流
            in = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");
            
            //2. 利用Properties里面的load方法加载文件
            props.load(in);
            
            //3. 可以通过Properties类对象,获取到想要的数据
            url = props.getProperty("url");
            user = props.getProperty("user");
            password = props.getProperty("password");
            driverClass = props.getProperty("driver");
            
            //4. 加载类文件
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            // TODO: handle exception
            e.printStackTrace();
            System.out.println("驱动加载失败");
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("驱动加载失败");
        } finally {
            //关闭文件连接
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 获取数据库连接对象
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            
        return conn;
    }
    
    /**
     * 关闭数据库连接,释放Statement
     * @param conn 数据库连接对象
     * @param st Statement对象
     */
    public static void close(Connection conn, PreparedStatement ps) {
        try {
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
            //糖衣炮弹
            throw new RuntimeException(e);
        }
    }
    /**
     * 关闭带有结果集的查询语句资源
     * @param conn
     * @param st
     * @param set
     */
    public static void close(Connection conn, PreparedStatement ps, ResultSet res) {
        try {
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
            if (res != null) {
                res.close();
            }
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
            //糖衣炮弹
            throw new RuntimeException(e);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值