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 起因 程序的核心是数据和逻辑,开发过程中免不了要对操作的数据进行设置,而有些数据在程序执行过程中被用户或程序做出的修改是应该保存下来的,这样程序 ...
随机推荐
- 管理Mac的Python环境
问题描述 我的Mac自带了版本为2.7.10的Python却没有用于管理依赖的pip工具.而我在使用刚开始学习Python时,从其官网下载了安装脚本安装了3.6版本的Python.脚本自动配置了环境变 ...
- 济南清北学堂游记 Day 0.
(摄于千佛山山顶,济南城区风光) 看似稳得一比,实则慌如老狗= = 我可能是报到最早的且实力最弱的一只. 早晨六点二十被从床上拉起来,然后在火车站附近匆忙吃了点东西就坐火车去济南了. 路途不算远,大概 ...
- BZOJ 4516: [Sdoi2016]生成魔咒 [后缀自动机]
4516: [Sdoi2016]生成魔咒 题意:询问一个字符串每个前缀有多少不同的子串 做了一下SDOI2016R1D2,题好水啊随便AK 强行开map上SAM 每个状态的贡献就是\(Max(s)-M ...
- BZOJ 1040: [ZJOI2008]骑士 [DP 环套树]
传送门 题意:环套树的最大权独立集 一开始想处理出外向树树形$DP$然后找到环再做个环形$DP$ 然后看了看别人的题解其实只要断开环做两遍树形$DP$就行了...有道理! 注意不连通 然后洛谷时限再次 ...
- 万类之父——Object
jdk1.8.0_144 Object类作为Java中的顶级类,位于java.lang包中.所有的类直接或者间接都继承自它.所以Object类中的方法在所有类中都可以直接调用.在深入介绍它的API时, ...
- Windows上MongoDB的安装
下载 下载MongoDB,自然是在他的官网下载相应系统的版本了,我下载的是Windows的安装包. 安装 傻瓜式安装,不多说.(默认安装在C盘的.) 启动 mongodb的数据存在一个叫data 文件 ...
- vuex是什么东西?
vuex是什么鬼? 文档上面对vuex的解释是 "一个专为 Vue.js 应用程序开发的状态管理模式",恩,看完这句是否对vuex有了一个大概的认识? 答案是:"认识你个 ...
- Angular4---部署---Angular 与 Nginx的邂逅
Nginx + Angular结合操作 1.下载Nginx , 根据自己的版本下载Nginx,关于Nginx配置,请看https://www.cnblogs.com/MBirds/p/6605366. ...
- JSON工具类
import java.sql.Timestamp; import java.util.Collection; import java.util.Date; import org.soul.util. ...
- LNMP搭建04 -- 配置Nginx支持PHP
首先建立存放网页文件的目录,执行 mkdri /usr/local/server/www 然后进入到该目录中 cd /usr/local/server/www 然后创建一个测试文件: phpinfo ...