该篇文章是讲述DBHelper(数据库连接类)调用xx.properties文件中的属性进行数据库连接。
在此之前我们先了解一下xx.properties是什么、用来干嘛的: .properties叫资源文件 属于配置文件,它用来存储一些属性值、一些数据(简单的讲),该文件的形式是键/值(Key/Value)对。
xx.properties在myeclipse或者eclipse中如何创建呢?
创建很简单就跟如何建.txt的一样该一下后缀名就可以了。如下图
就会出现db.properties编辑页面,然后我们敲上代码(如下):
# #是注释# properties 是以 Key/Value 存在 我的是Oracle数据库 记得要导数据库连接的jar#驱动jdbc.driver_class=oracle.jdbc.driver.OracleDriver#数据库地址jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl#账号jdbc.username=scott#密码jdbc.passWord=tiger这样我们就已经完成了db的配置。
我们在java中如何调用这个db.properties呢?
我们通过这个类 java.util.Properties来调用。
Properties 类:
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。
这个类我们要使用最主要的方法:
load ( InputStream inStream):void 用来读取inStream 中的文件,并将键值对存入Properties对象中。
list(Properties out):void 打印.properties中的键值对。
getProperty ( String key):String 根据Key查询Value。
(以上只是个人对方法认识并不是对该方法的正确认识,官方的认识方法可以上API。 这个网站也可以Java中Properties类的操作 。)
然后我们进行上代码:
Properties p=new Properties(); //创建Properties对象 try { String path=Thread.currentThread().getContextClassLoader().getResource ("b_map//db.properties").getPath(); System.out.println(path); /*在这里要重点讲一下path 获取.properties路径问题 * Thread.currentThread().getContextClassLoader().getResource (path).getPath(); * 上面的方法是 返回当前ClassPath的绝对路径。 * ------------------------------------------------------------ * p.load(new FileInputStream("db.properties")); 我们不能这样直接放如果这样放就会 * 出现一个错误java.io.FileNotFoundException: db.properties (系统找不到指定的文件。) * 具体为什么我也不怎么清楚。求解!!!! */ p.load(new FileInputStream(path));//读取.preperties中的信息 p.list(System.out);//System.out 是打印到控制台 System.out.println("****************************************"); System.out.println("账号:"+p.getProperty("jdbc.username"));//p.getProperty(Key) 根据Key查询Value System.out.println("密码:"+p.getProperty("jdbc.password")); System.out.println("驱动:"+p.getProperty("jdbc.driver_class")); System.out.println("数据库地址:"+p.getProperty("jdbc.url")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }可能有人会出现:java.io.FileNotFoundException: db.properties (系统找不到指定的文件。)我是把db.properties放到哪里的:如下图
我是放到包包下面的,所以
String path=Thread.currentThread().getContextClassLoader().getResource("b_map//db.properties").getPath();
这行代码的路径为 "b_map//db.properties",如果是放在 src 下面的话 路径为 "db.properties"。
运行代码结果:
好,这些测试我们都已经测试完毕了,进入最后的环节了,如何从db.properties中读取属性值,进行数据库连接。
代码如下:
public class DBHelper { public static void main(String[] args) { DBHelper d=new DBHelper(); Connection conn=d.getConn(); if(conn!=null){//如果不为空就证明读取db.properties中的属性成功了 System.out.println("OK"); } }// private static final String cname="oracle.jdbc.driver.OracleDriver"; //无关// private static final String curl="jdbc:oracle:thin:@localhost:1521:orcl";//无关 private static final Properties p=new Properties(); static{ try { //获取绝对路径 String path=Thread.currentThread().getContextClassLoader().getResource ("b_map//db.properties").getPath(); p.load(new FileInputStream(path));//读取db.properties 配置文件中的键值对 并存入 p 对象中 //p.list(System.out); //打印db.properties 配置文件中的键值对 Class.forName(p.getProperty("jdbc.driver_class")); //Class.forName(cname); //无关 } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } /** * 数据库连接 * @return */ public static Connection getConn(){ Connection conn=null; try { conn=DriverManager.getConnection(p.getProperty("jdbc.url"),p.getProperty("jdbc.username"),p.getProperty("jdbc.password"));// conn=DriverManager.getConnection(curl,"scott","tiger"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return conn; } /** * 关闭数据库连接 * @param conn * @param ps * @param rs */ public static void MyClose(Connection conn,PreparedStatement ps,ResultSet rs){ try { if(!conn.isClosed()&&conn!=null){ conn.close(); } if(ps!=null){ ps.close(); } if(rs!=null){ rs.close(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } /** * 关闭数据库连接 * @param conn * @param ps */ public static void MyClose(Connection conn,PreparedStatement ps){ MyClose(conn, ps, null); } }测试结果:
-------------------------------------------------------------------------------------------------------------------------------------------------------
还有获取路径的方法:
1,FileTest.class.getResource("")得到的是当前类FileTest.class文件的URI目录。不包括自己!2,FileTest.class.getResource("/")得到的是当前的classpath的绝对URI路径。3,Thread.currentThread().getContextClassLoader().getResource("")得到的也是当前ClassPath的绝对URI路径。4,FileTest.class.getClassLoader().getResource("")得到的也是当前ClassPath的绝对URI路径。5,ClassLoader.getSystemResource("")得到的也是当前ClassPath的绝对URI路径。详情请看这个网站 JAVA项目引用文件路径问题
如有哪里错误请告诉小白(我),谢谢。
新闻热点
疑难解答