java读取properties配置文件
2015-12-08 17:58
621 查看
一:在项目资源包src、config下新建dbconfig.properties文件
二:创建连接类
driver=oracle.jdbc.OracleDriver url=jdbc:oracle:thin:@localhost:1521:orcl user=pwd password=pwd
二:创建连接类
package uaap.webservice.util; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.ResourceBundle; import cn.com.pansky.usp4o.common.util.Debug; public class JDBCConnection { private Connection conn = null; /** * 建立连接(方式1) */ public Connection getConnect(){ try { Map map =getConnectProperties(); Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); String driver = null; String url = null; String user = null; String password = null; while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); // System.out.println("" + entry.getKey() + ":" + entry.getValue()); if(entry.getKey().equals("driver")){ driver=entry.getValue(); }else if (entry.getKey().equals("url")){ url=entry.getValue(); }else if(entry.getKey().equals("user")){ user=entry.getValue(); }else if(entry.getKey().equals("password")){ password=entry.getValue(); } } //连接 Class.forName(driver); conn=DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } /** * 获取数据连接属性 * @return key-value */ public static Map<String, String> getConnectProperties() { Map<String, String> map = new HashMap<String, String>(); InputStream in = JDBCConnection.class.getClassLoader() .getResourceAsStream("dbconfig.properties"); Properties prop = new Properties(); try { prop.load(in); Enumeration<?> allName = prop.propertyNames(); while (allName.hasMoreElements()) { String name = (String) allName.nextElement(); String value = (String) prop.get(name); Debug.println("===================="+name + ":" + value); map.put(name, value); } in.close(); } catch (IOException e) { e.printStackTrace(); } return map; } //=================================================================================== /** * 建立JDBC连接(方式2) */ public Connection getConnection() { ResourceBundle bundle = ResourceBundle.getBundle("dbconfig"); String driver = bundle.getString("driver"); String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password"); Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } }
相关文章推荐
- java中文件名必须和类名一致吗
- JavaSE_JavaSE中int、String的类型转换
- Java 用栈解决括号匹配问题
- java的使用技巧(二)
- Java编程思想学习心得(六)关系操作符的陷阱
- spring中proxy-target-class解释
- SpringMVC 接收ajax发送的数组对象
- [JAVA] 注解学习@interface
- Java里List取并集方法retainAll不能用来判断是否有重复数据!
- 多线程
- MapReduce性能优化_8. 优化MapReduce的用户JAVA代码
- 序列化和反序列化,怎么保证数据的正确性
- 最全Pycharm教程(12)——Pycharm调试器之Java脚本调试
- Eclipse 中 新建maven项目 无法添加src/main/java 问题
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
- Java线程池中的异常消失
- Java编程思想学习心得(五)自动递增和递减操作符的陷阱
- java语言的特性有什么
- java开发工具eclipse中@author怎么改
- eclipse中run cofigurations 里面 Arguments 的两种参数详解