在jdbc中使用properites文件进行使用
2016-06-01 16:17
225 查看
首先先在源代码中创建一个properites文件
然后再在你自己的代码中添加如下代码
如果你设置的properties文件为只读文件则使用这种方法
利用这个的第三种方法
db_url=jdbc\:mysql\://localhost\:3306/db_friend db_user=root db_password= db_driver=com.mysql.jdbc.Driver
然后再在你自己的代码中添加如下代码
private static String db_driver=""; private static String db_url=""; private static String db_user=""; private static String db_password=""; static{ Locale locale=Locale.getDefault();//获取国家 ResourceBundle bandle=ResourceBundle.getBundle("cn/lonecloud/demo/jdbc",locale);//获取这个文件的内容第一个参数为相对这个文件的路径 db_driver=bandle.getString("db_driver"); db_url=bandle.getString("db_url"); db_user=bandle.getString("db_user"); db_password=bandle.getString("db_password"); }
如果你设置的properties文件为只读文件则使用这种方法
import java.io.InputStream; import java.util.Properties; public class demo { public static void main(String[] args) { // TODO Auto-generated method stub Properties properties=new Properties();//创建Properties类 InputStream in=demo.class.getResourceAsStream("jdbc.properties");//获取文件流 try { properties.load(in);//将文件流导入properties中 } catch (Exception e) { // TODO: handle exception } System.out.println(properties.getProperty("db_url"));//获取键值 } }
利用这个的第三种方法
import java.io.InputStream; import java.util.Properties; public class demo { public static void main(String[] args) { // TODO Auto-generated method stub Properties properties=new Properties();//创建Properties类 InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");//获取文件流 try { properties.load(in);//将文件流导入properties中 } catch (Exception e) { // TODO: handle exception } System.out.println(properties.getProperty("db_url"));//获取键值 } }
相关文章推荐
- Linux信号说明列表
- docker隔离与cgrep
- nova mitaka ReleaseNotes
- Open CV缩放图像
- 进度条的实现
- 《Linux总线、设备与驱动》USB设备发现机制
- centos7 修改yum源为阿里源
- Keepalived+Nginx实现负载均衡高可用
- 通过linux ssh远程登录另一台Linux,无需密码,用证书验证
- 解决linux不能连外网
- 网站页面性能优化的34条黄金守则
- 用Apache Spark进行大数据处理——第二部分:Spark SQL
- 《Linux总线、设备与驱动》USBHID设备驱动
- nodejs在Linux下使用图片相关模块出现Error: write EPIPE
- 利用OpenCV的threshold函数作图像的固定阈值化处理!
- shell编程学习
- Linux分文件命令
- 去掉tomcat配置文件中的注释选项
- Jenkins + Github持续集成构建Docker容器 推荐
- Linux下编译Android源码问题汇总