JDBC中怎样隐藏数据库的名字

解决方案 »

  1.   

    可以把数据库名、用户名、密码等敏感信息放在服务器端禁止网络访问的目录下,用到时再把它们读出,下面是我常用的个工具类,其实是从java cookbook修改来的:
    package pputil;import java.io.*;
    import java.util.*;/**
     * <p>Title: FileProperty</p>
     * <p>Description: 从文件读Property值</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: ZSU</p>
     * @author Phil
     * @version 1.0
     */public class FileProperty
        extends Properties {
      private String fileName = null;
      private InputStream inStr = null;
      private OutputStream outStr = null;  /** 由给定的文件名构造类 */
      public FileProperty(String loadsaveFileName) throws IOException {
        super();
        fileName = loadsaveFileName;
        load();
      }  /**
       * 从指定的文件载入属性值,如果失败,确保后缀名为".properties",
       * 然后再重试一次
       */
      public void load() throws IOException {
        try {
          if (inStr == null) {
            inStr = new FileInputStream(fileName);
          }
        }
        catch (FileNotFoundException fnf) {
          if (!fileName.endsWith(".properties")) {
            inStr = new FileInputStream(fileName + ".properties");
            // 如果成功,则记住改变后的文件名
            fileName += ".properties";
          }
          else {        // 否则抛出错误
            throw fnf;
          }
        }
        //调用超类的load函数
        load(inStr);
      }  /** 保存属性值 */
      public void save() throws IOException {
        if (outStr == null) {
          outStr = new FileOutputStream(fileName);
        }
        store(outStr, "# Written by FileProperty.save() at " + new Date());
      }  /** 关闭流 */
      public void close() {
        try {
          if (inStr != null) {
            inStr.close();
          }
          if (outStr != null) {
            outStr.close();
          }
        }
        catch (IOException e) {
          e.printStackTrace();
        }
      }
    }把信息写入SQLconnection.properties文件,可以用下面的代码得到:
        FileProperty sqlProperty=null;    try {      sqlProperty = new FileProperty("SQLconnection.properties");    }    catch (IOException ex) {      ex.printStackTrace();    }    String user=sqlProperty.getProperty("user");    String password=sqlProperty.getProperty("password");    sqlProperty.close();完整的信息可以访问
    http://www.blogcn.com/user41/blog_of_phil/index.html