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属性

解决方案 »

  1.   

    你在读取properties时改他们的引用不就可以了吗
      

  2.   


    url=jdbc:mysql://${host}/student 
    是这个意思吗?
      

  3.   

    你要用 ${host} 也行,用 #{host} 也行,没有现成的 API,自己解析去吧
      

  4.   

    楼主何必多此一举,如果你的host是变化的,那必定要改properties文件中的属性性,既然要改,那就直接改url好了,还要host干什么,要多写一个key/value,不是反而麻烦了吗?
      

  5.   

    有个地方又要用单独的host属性值啊
      

  6.   

    好像不行啊
    读成了
    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());
    }
    }
    没用过这东西呀