JavaWeb中jdbcproperties配置文件
开发中使用properties配置文件,方便后期维护。
- 文件位置: 任意,建议src下
- 文件名称:任意,扩展名为properties
- 文件内容:一行一组数据,格式“key=value”
- key 命名自定义,如果是多单词,习惯使用点分割,例如jdbc.driver
- value 值不支持中文,如果有需要使用非英文字符,将进行Unicode转化
配置文件只需要加载一次,提供静态代码,当前类被加载到内存执行
package com.jdbc.dao; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle; public class JdbcUtils { private static String user;
public static String password;
public static String className;
public static String url;
Connection con=null;
PreparedStatement pstm=null;
ResultSet rs=null;
static{
//getBundle("properties配置文件的名称");
ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
// getObject("properties配置文件的key值");
user=bundle.getString("jdbc.user");
password=bundle.getString("jdbc.password");
className=bundle.getString("jdbc.className");
url=bundle.getString("jdbc.url"); }
public JdbcUtils() {
try{
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} } public Connection getConnection() {
try {
con=(Connection) DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
con=null;
e.printStackTrace(); } return con;
} public ResultSet excuteQuery(String sql, Object[] obj) {
if (sql!=null ){
con=getConnection();
if(con!=null){
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
if (obj!=null) {
for(int i=0;i<obj.length;i++){
pstm.setObject(i+1,obj[i]);
}
} rs=pstm.executeQuery(); } catch (SQLException e) {
e.printStackTrace();
}
}
} return rs;
} public int excuteUpdate(String sql, Object[] obj) {
// TODO Auto-generated method stub
int flag=-1;
if(sql!=null && obj!=null){
con=getConnection(); if (con!=null) {
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
for(int i=0;i<obj.length;i++){
pstm.setObject(i+1, obj[i]);
}
flag=pstm.executeUpdate(); } catch (SQLException e) {
e.printStackTrace();
}
}
}
return flag;
} public ResultSet queryAll(String sql) { con=getConnection();
if(con!=null){
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
rs=pstm.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
} return rs; } public void closeAll() {
// TODO Auto-generated method stub
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pstm!=null) {
try {
pstm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
配置文件 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bookstore
加载配置文件:properties对象
开发中会使用Properties对象进行, 我们可以采用加载properties 文件获得流,然后使用Properties对象进行处理
1. 加载properties文件获取inputStream
1.1 方式1.使用类加载ClassLoader加载src的资源(固定写法) 获得ClassLoader固定写法:当前类.class.getClassLoader();
InputStream is= jdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
// InputStream is=本类名.class.getClassLoader().getSourceAsStream("properties配置文件名称");
1.2 方式2 加载当前类同包下的资源,如果需要从src开始必须填写 ‘’/‘’
InputStream is2=jdbcUtil.class.getResourceAsStream("jdbc2.properties");
加载src下的资源
InputStream is3=jdbcUtil.class.getResourceAsStream("/jdbc.properties");
加载完成后:使用Properties处理流
Properties props=new Properties();
使用load() 方法加载指定的流
props.load(is); / props.load(is3); / props.load(is2);
Properties props=new Properties();
props.load(is);
//使用getProperty(key),获取需要的值
className=props.getProperty("jdbc.className");
url=props.getProperty("jdbc.url");
user=props.getProperty("jdbc.user");
password=props.getProperty("jdbc.password");
jdbc加载项目
package com.jdec.util_v3; import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties; public class jdbcUtil {
private static String user;
public static String password;
public static String className;
public static String url;
Connection con=null;
PreparedStatement pstm=null;
ResultSet rs=null;
static{
//1.加载properties文件获取inputStream
/*1.1 方式1.使用类加载ClassLoader加载src的资源(固定写法)
* 获得ClassLoader固定写法:当前类.class.getClassLoader();
*/
InputStream is= jdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//加载当前类同包下的资源,如果需要从src开始必须填写/
InputStream is2=jdbcUtil.class.getResourceAsStream("jdbc2.properties");
//加载src下的资源
InputStream is3=jdbcUtil.class.getResourceAsStream("/jdbc.properties"); //使用Properties处理流
// 使用load() 方法加载指定的流
Properties props=new Properties();
try {
props.load(is);
//使用getProperty(key),获取需要的值
className=props.getProperty("jdbc.className");
url=props.getProperty("jdbc.url");
user=props.getProperty("jdbc.user");
password=props.getProperty("jdbc.password"); } catch (IOException e) {
e.printStackTrace();
} }
public jdbcUtil() {
try{
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} } public Connection getConnection() {
try {
con=(Connection) DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
con=null;
e.printStackTrace(); } return con;
} public ResultSet excuteQuery(String sql, Object[] obj) {
if (sql!=null ){
con=getConnection();
if(con!=null){
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
if (obj!=null) {
for(int i=0;i<obj.length;i++){
pstm.setObject(i+1,obj[i]);
}
} rs=pstm.executeQuery(); } catch (SQLException e) {
e.printStackTrace();
}
}
} return rs;
} public int excuteUpdate(String sql, Object[] obj) {
// TODO Auto-generated method stub
int flag=-1;
if(sql!=null && obj!=null){
con=getConnection(); if (con!=null) {
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
for(int i=0;i<obj.length;i++){
pstm.setObject(i+1, obj[i]);
}
flag=pstm.executeUpdate(); } catch (SQLException e) {
e.printStackTrace();
}
}
}
return flag;
} public ResultSet queryAll(String sql) { con=getConnection();
if(con!=null){
try {
pstm=(PreparedStatement) con.prepareStatement(sql);
rs=pstm.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
} return rs; } public void closeAll() {
// TODO Auto-generated method stub
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pstm!=null) {
try {
pstm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
测试类
package com.jdbc.util; import java.sql.ResultSet;
import java.sql.SQLException; import org.junit.Test; import com.jdbc.dao.JdbcUtils;
import com.jdec.util_v3.jdbcUtil; public static void main(String[] args) throws SQLException{
jdbcUtil jd=new jdbcUtil();
String sql="select * from book";
Object[] obj=null;
ResultSet rs=jd.excuteQuery(sql, obj);
while(rs.next()){
System.out.println(rs.getObject("bookId")+" "+rs.getObject("bookName")+" "+rs.getObject("bookAuthor")); }
}
}
jdbc.properties
jdbc.user=root
jdbc.password=root
jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bookstore
JavaWeb中jdbcproperties配置文件的更多相关文章
- 1、javaweb学习之配置文件web.xml
今天这里主要讲述javaweb中的配置文件web.xml中的内容及其作用,都是基础部分,对于初学者需要好好掌握理解. 简单配置: <servlet> <servlet-name ...
- JavaWeb中servlet读取配置文件的方式
我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...
- 在JavaWeb中使用Log4j步骤
在JavaWeb中使用Log4J指南.每次在开始写一个项目的时候都忘记Log4J如何配置.所以写个步骤,作为记录. 第一步 下载Log4J jar包 从Apache Logging Services ...
- 在javaweb中通过servlet类和普通类读取资源文件
javaweb有两种方式读取资源文件 在Servlet中读取,可以使用servletContext,servletContext可以拿到web所有的资源文件,然后随便读,但是这种方法不常用,尽量少在S ...
- JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
JavaWeb中监听器+过滤器+拦截器区别.配置和实际应用 1.前沿上一篇文章提到在web.xml中各个元素的执行顺序是这样的,context-param-->listener-->fil ...
- JavaWeb中的监听器
JavaWeb中的监听器 l 事件源:三大域! ServletContext ¨ 生命周期监听:ServletContextListener,它有两个方法,一个在出生时调用,一个在死亡时 ...
- 在开发中关于javaweb中的路径问题小结
转自http://blog.csdn.net/yinyuehepijiu/article/details/9136117 在javaweb项目中添加配置文件,满足连接数据库配置参数以及其他自定义参数存 ...
- javaWEB中的四种域对象
javaWEB中的四种域对象 (1)ServletContext ServletContext是最大的Web域对象,在整个工程内有效,可以存储一些需要全局部署的配置文件,也可以存储其他信息,不过因为它 ...
- C#开发中使用配置文件对象简化配置的本地保存
C#开发中使用配置文件对象简化配置的本地保存 0x00 起因 程序的核心是数据和逻辑,开发过程中免不了要对操作的数据进行设置,而有些数据在程序执行过程中被用户或程序做出的修改是应该保存下来的,这样程序 ...
随机推荐
- git指令总结
在学习flask之前,先汇总一下Git的指令. mkdir filedir 创建文件夹filedir cd filedir 进入文件夹 pwd 显示当前工作目录 git init 初始化git仓库 g ...
- Go笔记-map
[概念] 1- map 是引用类型的 2- 声明方式 var map1 map[keytype]valuetype 例如:var map1 ma ...
- 在eclipse的配置文件里指定jdk路径
在eclipse的配置文件里指定jdk路径,只需在eclipse的配置文件里增加-vm参数即可. 打开eclipse目录下的eclipse.ini配置文件,增加-vm配置,需要注意的是该参数要加在-v ...
- VUE 2.0 引入高德地图,自行封装组件
1. 高德地图官网 申请帐号, 申请相应(JavaScript API)的 Key 2. 在项目中引入, 这里和其他的引入不同的是 直接在 index.html, 不是在 main.js 引入, 博主 ...
- python爬站长之家写一个信息搜集器
前言:不知道写什么好,绕来绕去还是写回爬虫这一块. 之前的都爬了一遍.这次爬点好用一点的网站. 0x01: 自行备好requests模块 目标站:http://tool.chinaz.com/ 0x2 ...
- EL表达式多条件判断方式
<td> <c:forEach items="${cityMap}" var="entry"> <hr> <input ...
- Sourcetree的安装与使用
1 安装遇到的问题 https://segmentfault.com/q/1010000007643870 解决该问题的方法: http://www.jianshu.com/p/3478e2a214a ...
- Install Centrifugo and quick start
Install Centrifugo and quick start Go is a perfect language - it gives developers an opportunity to ...
- Linux中7个用来浏览网页和下载文件的命令
上一篇文章中,我们提到了rTorrent.wget.cURL.w3m.Elinks等几个有用的工具,很多人回信说还有其它几个类似的工具也值得讨论,所以就有了这篇文章.如果错过了第一部分的讨论,可以通过 ...
- ansible安装
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 1.配置epel源 wget -O /etc/yum.repos.d ...