properties + IO 读取配置文件
1.项目目录树

2.配置文件config.properties
username = sushe
password = sushe
url = jdbc:mysql://172.16.100.10:3306/sushe?useUnicode=true&characterEncoding=GBK
jdbcDriverName = com.mysql.jdbc.Driver
3.JdbcBean对象,用来存储jdbc链接信息
package com.dlab.bean;
public class JdbcBean {
private String userName;
private String password;
private String Url;
private String jdbcDriverName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public String getJdbcDriverName() {
return jdbcDriverName;
}
public void setJdbcDriverName(String jdbcDriverName) {
this.jdbcDriverName = jdbcDriverName;
}
}
4.读取配置文件工具JdbcConfig.java
package com.dlab.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.dlab.bean.JdbcBean;
public class JdbcConfig {
private JdbcBean jdbcBean;
public JdbcConfig() throws IOException{
//使用InputStream读取配置文件
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
Properties configReader = new Properties();
//加载配置文件
configReader.load(in);
jdbcBean = new JdbcBean();
jdbcBean.setUserName(configReader.getProperty("username"));
jdbcBean.setPassword(configReader.getProperty("password"));
jdbcBean.setUrl(configReader.getProperty("url"));
jdbcBean.setJdbcDriverName(configReader.getProperty("jdbcDriverName"));
}
//返回JdbcBean对象
public JdbcBean getConfigInfo(){
return jdbcBean;
}
}
5.JDBC工具DBUtil.java,获取Connection
package com.dlab.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.dlab.bean.JdbcBean;
public class DBUtil {
/**
* 采用单例模式
*/
private static DBUtil instance = new DBUtil();
private Connection conn;
private DBUtil (){
JdbcBean jdbcBean;
try {
jdbcBean = new JdbcConfig().getConfigInfo();
Class.forName(jdbcBean.getJdbcDriverName());
conn = DriverManager.getConnection(jdbcBean.getUrl(), jdbcBean.getUserName(), jdbcBean.getPassword());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static synchronized DBUtil getInstance (){
return instance;
}
//返回Connection
public Connection getConn(){
return conn;
}
//Connection的静态关闭方法
public static void close(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//PreparedStatement的静态关闭方法
public static void close(PreparedStatement pstmt){
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//ResultSet的静态关闭方法
public static void close(ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
6.测试程序DBTest.java
package com.dlab.dbtest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.dlab.util.DBUtil;
public class DBTest {
public static void main(String[] args) {
String sql = "select * from Admin";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getInstance().getConn();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("Admin_Username"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
}
}
}
properties + IO 读取配置文件的更多相关文章
- 使用Properties去读取配置文件,并获得具体内容值
有时候,写了一个配置文件,需要知道读出来的内容对不对,我们需要测试一下,看看读出来的跟我们要的是不是一样.这里写了一个工具类,用来读取配置文件里面的内容. 一.使用Properties工具类来读取. ...
- Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】
Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...
- java properties类读取配置文件
1.JAVA Properties类,在java.util包里,具体类是java.util.properties.Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值 ...
- 【Properties文件】Java使用Properties来读取配置文件
配置文件位置及内容 执行结果 程序代码 package Utils.ConfigFile; import java.io.BufferedInputStream; import java.io.B ...
- Properties类读取配置文件
package com.wzy.t4; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- 对Java配置文件Properties的读取、写入与更新操作
http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties 对Jav ...
- 实现对Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- ResourceBundle与Properties读取配置文件
ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取. ResourceBundl ...
随机推荐
- IntelliJ IDEA 13 破解安装(JRebel 5.6.3a皴)
首先安装IntelliJ 13,记住下载Ultimate Edition版本号,否则就必须打破.. 安装到本地.然后一些配置(这一步不能.不过考虑到交换系统后,保存,建议做) 打开{install ...
- leetcode第12题--Integer to Roman
Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...
- PhotoShop基本工具 -- 移动工具
艺术或学习的东西吧, 爱好 比学编程还难 PS版本号 : PhotoShop CS6 1. 移动工具 (1) 工具栏和属性栏 工具栏 和 属性栏 : 左側的是工具栏, 每选中一个工具, 在菜单条的 ...
- Android 发展 ------------- Unable to resolve target 'android-19'
又一次装完Ecplise+ATD+Android SDK 在Ecplise工作空间导入之前写过的Android项目会出现错误,大部分是SDK 版本号不符,例如以下错误提示: Error:Unable ...
- 反射导出excel案例
1.代码案例: protected void btnExportExcel_Click(object sender, EventArgs e) { SetSearchValue(); Dictiona ...
- 使用ServletContext实现数据共享和获得web.xml中的参数
//适用于:很多文件需要初始化参数时,例如数据库账号和密码,不可能使用config这个对象,因为如果使用config对象去配置的话,那么每个servlet类都必须写一个参数,这时候就必须采用conte ...
- Scala + Play + Sbt + Protractor
Scala + Play + Sbt + Protractor = One Build 欢迎关注我的新博客地址:http://cuipengfei.me/ 我所在的项目的技术栈选用的是Play fra ...
- Web Api的安全性
Web Api的安全性 系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 这一篇文章我们主要来探讨一下Web Api的安全性,到目前为止所有的 ...
- dedecms模板中使用php代码
{dede:php} echo “test”: {/dede:php} 使用这段代码之前要在后台的系统--系统基本参数--其它选项 里找到 模板引擎禁用标签: php 将其删除
- Python 用SMTP发送邮件
一.简介 上面介绍了传统邮件的生成和解析,这些都是non-internet,也就是不需要网络就可一完成的.那么当生成了邮件,下一步就是发送了,本文就讲解利用SMTP协议发送邮件. 正如SMTP(Sim ...