java加载配置文件

     

java可以通过Properties类来加载配置文件,该全限定名是java.util.Properties  .

                public synchronized void load(InputStream inStream) throws IOException通过该方法来加载配置文件

                使用public String getProperty(String key) 方法来获得配置文件中的字段

下面是一段加载数据库的配置文件的一段代码
package com.dao;
import java.sql.Statement;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DaoImpl {
	private String driver;//驱动
	private String url;//数据库路径
	private String user;//用户
	private String password;//用户密码
	
	
	//初始化数据库
	public void initDataBase(String param) throws FileNotFoundException, IOException{
		//产生配置文件类
		Properties pro = new Properties();
		//加载配置文件
		pro.load(new FileInputStream(param));
		
		//获取配置
		driver = pro.getProperty("driver");
		url = pro.getProperty("url");
		user = pro.getProperty("user");
		password = pro.getProperty("password");
		
	}
	
	//执行sql查询语句
	public void executedSql(String sql,int i ) throws SQLException, ClassNotFoundException{
		
		long start = System.currentTimeMillis();
		//加载数据库驱动
		Class.forName(driver);
		//获取链接 
		Connection conn = DriverManager.getConnection(url,user,password);
		Statement statement = conn.createStatement();
		
		ResultSet result = statement.executeQuery(sql);
		while(result.next()){
			System.out.println(result.getString(1) + "\t" + result.getString(2));
		}
		long end = System.currentTimeMillis();
		System.out.println("the " + i + " times lasts time : " + (end - start));
	}
	public static void main(String[] args)
			throws FileNotFoundException, IOException, SQLException, ClassNotFoundException{
		DaoImpl dao = new DaoImpl();
		dao.initDataBase("G:/mysqlx.properties");
		for(int i = 0 ; i < 100; i++){
			dao.executedSql("select * from student",i);
		}
	}	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值