driver=com.mysql.jdbc.Driver
host=127.0.0.1
database=student
url=jdbc:mysql://127.0.0.1/student
user=root
password=123456如上我想让url属性引用host属性
host=127.0.0.1
database=student
url=jdbc:mysql://127.0.0.1/student
user=root
password=123456如上我想让url属性引用host属性
url=jdbc:mysql://${host}/student
是这个意思吗?
读成了
jdbc:mysql://#{host}/#{student}难道在读的时候有讲究吗?
我是这样读的:
JDBC.properties:
driver=com.mysql.jdbc.Driver
host=127.0.0.1
database=student
url=jdbc:mysql://#{host}/#{student}
user=root
password=123456
package com.whvcse;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 读取JDBC.properties文件中属性值的专用类
* 这是从网上copy的类修改而成的,他说很安全简单就直接用了,虽然有点不明白。
* <br>
* <a href="http://jeasony.javaeye.com/blog/292075">从这里copy的</a>
* @author XXX
*
*/
public final class ReadProperty { private static String driver;
private static String host;
private static String database;
private static String url;
private static String user;
private static String password;
static {
Properties p = new Properties();
InputStream in = ReadProperty.class.getResourceAsStream("JDBC.properties");
try {
p.load(in);
driver = p.getProperty("driver").trim();
host = p.getProperty("host").trim();
database = p.getProperty("database").trim();
url = p.getProperty("url").trim();
user = p.getProperty("user").trim();
password = p.getProperty("password").trim();
} catch (IOException e) {
e.printStackTrace();
}
} private ReadProperty() { // 私有构造方法,不需要创建对象
}
/**
* 读取JDBC.properties中driver属性的值
* @return driver属性的值
*/
public static String getDriver() {
return driver;
}
/**
* 读取JDBC.properties中host属性的值
* @return host属性的值
*/
public static String getHost() {
return host;
}
/**
* 读取JDBC.properties中database属性的值
* @return database属性的值
*/
public static String getDatabase() {
return database;
}
/**
* 读取JDBC.properties中url属性的值
* @return url属性的值
*/
public static String getUrl() {
return url;
}
/**
* 读取JDBC.properties中user属性的值
* @return user属性的值
*/
public static String getUser() {
return user;
}
/**
* 读取JDBC.properties中password属性的值
* @return password属性的值
*/
public static String getPassword() {
return password;
}
public static void main(String[] args) {
System.out.println(ReadProperty.getUrl());
}
}
没用过这东西呀