properties

类为java是跨平台的,所有配置文件不可能是绑定到任何一个平台上的,其配置文件也要有自己的一套东西.
不可能是.ini等这样的格式,而是properties.
1。 使用java.util.Properties类的load()方法示例:InputStreamin=lnewBufferedInputStream (newFileInputStream(name));Propertiesp=newProperties();p.load(in);

  2。使用java.util.ResourceBundle类的getBundle()方法示例:ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());

   3。使用java.util.PropertyResourceBundle类的构造函数示例:InputStreamin= newBufferedInputStream(newFileInputStream(name));ResourceBundlerb=newPropertyResourceBundle(in);

   4。使用class变量的getResourceAsStream()方法示例:InputStreamin= JProperties.class.getResourceAsStream(name);Propertiesp=newProperties();p.load(in);

   5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream ()方法示例:InputStreamin=JProperties.class.getClassLoader(). getResourceAsStream(name);Propertiesp=newProperties();p.load(in);

   6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法示例: InputStreamin=ClassLoader.getSystemResourceAsStream(name);Propertiesp=newProperties();p.load(in);

  补充

   Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法示例: InputStreamin=context.getResourceAsStream(path);Propertiesp=newProperties();p.load(in); 

代码实例:
---------------------------------------------------------------------------------------------
package com.xing.db;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class LoadConn {
    String name="";
    String password="";
    String driver ="";
    String str ="";
    java.sql.Connection conn;
    private java.sql.Connection getConnection(){
        InputStream in = this.getClass().getResourceAsStream("conn.properties");
        Properties p = new Properties();
        try {
            p.load(in);
            name=p.getProperty("name");
            password = p.getProperty("password");
            driver = p.getProperty("driver");
            str = p.getProperty("str");
            System.out.println(name+"  "+driver);
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(str, name, password);
                if(conn!=null){
                    System.out.println("Connection successful");
                }
            } catch (ClassNotFoundException e) {
                // TODO 自动生成 catch 块
                e.printStackTrace();
            } catch (SQLException e1) {
                // TODO 自动生成 catch 块
                e1.printStackTrace();
            }
        } catch (IOException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return conn;
    }
    public static void main(String[] args) {
        LoadConn lc = new LoadConn();
        lc.getConnection();

    }

}






properties文件的使用

properties文件顾名思义,属性文件,从它的名称中直观的理解就是,它应该是可以表示某些属性,

是的,可以在它里面定义一些字段,这将不需要我们在代码中书写,这就可以将这些信息从代码中分

离出来了,很方便。我是在编写数据库代码的过程中接触到这个文件的,感觉确实不错!


下面我给出两个例子,直观的看一下两者的区别:
首先我先将properties文件给出,文件名为myProperties.properties

String driver = com.mysql.jdbc.Driver
String url = jdbc:mysql://localhost/myDB;
String name = root
String password = 123


例子一:
String driver = "com.mysql.jdbc.Driver"
String url = "jdbc:mysql://localhost/myDB";
String name = "root"
String password = "123"

Class.forName(driver);
Connection con = DriverManager.getConnection(url,name,password);


例子二:
String driver;
String url;
String name;
String password;

FileInputStream fis = new FileInputStream(myProperties.properties);
Properties properties = new Properties();
properties.load(fis);            //从输入流中读取属性文件的内容
fis.close();
//从属性文件中读取相应字段的信息
driver = properties.getProperty(driver);
url = properties.getProperty(url);
name = properties.getProperty(name);
password = properties.getProperty(password);

Class.forName(driver);
Connection con = DriverManager.getConnection(url,name,password);


我们看到数值和代码已经分离,这样很方便我们修改数值!
再有一定要注意properties文件中的字段的写法,不要再多添“”否则会出现问题!
因为getProperty()方法返回的是一个字符串!

我想应该再对这个文件做一下加密处理会更好一些是吧?

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值