用static修饰的变量实例变量则是在类体里定义但没有static修饰的。类方法是有static修饰的方法。因为static是在类初始化时已经生成的。
那是属性的意思啊
就是我说的类属性跟实例变量了。
java.util.Properties类中有方法
Object
setProperty(String key,
String value)
Calls the Hashtable method put.
void
store(OutputStream out,
String comments)
Writes this property list (key and element pairs) in this
Properties table to the output stream in a format suitable
for loading into a Properties table using the
load(InputStream) method.
void
store(Writer writer,
String comments)
Writes this property list (key and element pairs) in this
Properties table to the output character stream in a
format suitable for using the load(Reader)
method.
void
storeToXML(OutputStream os,
String comment)
Emits an XML document representing all of the properties contained
in this table.
void
storeToXML(OutputStream os,
String comment,
String encoding)
Emits an XML document representing all of the properties contained
in this table, using the specified encoding.
private static String driver =nullprivate static String url = null
private static String user = null
private static String password = null
private static BasicDataSource ds
static{
//读取程序外的.properties 文件
//需要.properties文件的包路径
Properties props = new Properties()
try {
String path ="db.properties"
props.load(
DBUtils.class.getResourceAsStream(path)
)
//properties对象.getProperty("字符串")
driver=props.getProperty("driver")
url=props.getProperty("url")
user=props.getProperty("user")
password=props.getProperty("password")
ds = new BasicDataSource()
ds.setDriverClassName(driver)
ds.setUrl(url)
ds.setUsername(user)
ds.setPassword(password)
Class.forName(driver)
} catch (Exception e) {
e.printStackTrace()
}
}
这是一个JDBC读取配置文件连接数据库的示例代码,供参考!