Properties类的使用方法
来源:互联网 发布:c语言中double 编辑:程序博客网 时间:2024/06/05 18:08
用于读取.properties文本文件的类
properties文件是一个纯文本文件,里面定义的内容格式有要求,
必须是key=value的形式,并且以行为单位,
一行只记录一条数据,下图是一个properties的书写案例
Properties类可以方便的读取properties文件,并将内容以类似HashMap的形式进行读取。注意!读取的都是字符串!
Properties通常都是配置文件而使用的。
* 通过Properties的方法getProperty(String key)
* 方法可以将properties文件中
* jdbc.driver=oracle.jdbc.driver.OracleDriver
* 数据获取。获取方式是将jdbc.driver以key作为参数调用方法
* 返回的就是等号右面的值oracle.jdbc.driver.OracleDriver
下例是相应的实例程序(下图是properties的放置位置):
package day02.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* DAO父类
* 基础类
* 提供所有DAO都需要具备的特性
* @author Ivershens
*
*/
public class BaseDAO {
/**
* java.util.Properties类
* 用于读取.properties文本文件的类
* properties文件是一个纯文本文件,里面定义的内容格式有要求
* 必须是key=value的形式,并且以行为单位
* 一行只记录一条数据
* Properties类可以方便的读取properties文件,并将内容
* 以类似HashMap的形式进行读取。注意!读取的都是字符串!
*
* Properties通常都是配置文件而使用的。
*/
private static Properties properties = new Properties();
// private static String driver = "oracle.jdbc.driver.OracleDriver";
// private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
// private static String user = "scott";
// private static String pwd = "scott";
private static String driver;
private static String url;
private static String user;
private static String pwd;
/**
* 在静态初始化中注册驱动
* 驱动不需要重复注册,所以静态初始化最合适注册驱动
*/
static {
try {
/**
* 加载配置文件,读取配置信息
*/
properties.load(BaseDAO.class.getClassLoader().getResourceAsStream("day02/dao/db.properties"));
System.out.println(properties.getProperty("jdbc.driver"));
/**
* 通过Properties的方法getProperty(String key)
* 方法可以将properties文件中
* jdbc.driver=oracle.jdbc.driver.OracleDriver
* 数据获取。获取方式是将jdbc.driver以key作为参数调用方法
* 返回的就是等号右面的值oracle.jdbc.driver.OracleDriver
*/
driver = properties.getProperty("jdbc.driver");
url = properties.getProperty("jdbc.url");
user = properties.getProperty("jdbc.user");
pwd = properties.getProperty("jdbc.pwd");
//加载驱动
Class.forName(driver);
/**
* oracle.jdbc.driver.OracleDriver类
* 在Class.forName()的时候被载入JVM
* OracleDriver是JDBC中的Driver的子类
* 它被要求在静态初始化的时候要将自身驱动的信息
* 通过DriverManager的静态方法注册进去,这样DriverManager
* 就知道应该如何通过OracleDriver去连接数据库了。
* 所以之后就可以通过DriverManager的另一个静态方法:
* getConnection()来根据之前注册的驱动信息获取连接了
*
* Connection conn = DriverManager.getConnection("", "", "");
*/
} catch (Exception e) {
e.printStackTrace();
//若注册失败,我们要通知调用者
throw new RuntimeException(e);
}
}
/**
* 获取数据库连接对象Connection
* @return
* @throws SQLException当连接失败时抛出该异常
*/
protected static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, pwd);
}
/**
* 将给定的数据库连接关闭
* @param conn
*/
protected static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- Properties类的使用方法
- Properties类的使用方法
- log4j.properties的作用与使用方法
- log4j.properties的作用与使用方法
- Properties使用方法解析
- java.util.properties使用方法
- log4j.properties 使用方法总结
- java项目中自定义的properties文件的使用方法
- Properties 类的使用
- Properties 类的使用
- properties类的使用
- Properties类的用法
- Properties类的使用
- 类 Properties 的用法
- Properties类的学习
- Properties类的使用
- Java的Properties类
- Properties类的使用
- codeforces 613D
- PHP的简单队列程序,写文件实现队列
- C++新手之全局变量
- 装饰大楼(详解+代码)
- 在pring基础上实现自定义异常处理框架教程
- Properties类的使用方法
- UI 开发
- 虚幻4 各个平台的入口函数----------Launch模块
- HDU1241-经典dfs
- How to use 'cp' command to exclude a specific directory?
- 关于QtGraphics编程的几点经验总结_qgraphicsscene
- Apple quietly slips WebRTC audio, video into Safari's WebKit spec
- hdu 5495 LCS
- Android API Guides---App Manifest <supports-screens> <uses-configuration>