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 起因 程序的核心是数据和逻辑,开发过程中免不了要对操作的数据进行设置,而有些数据在程序执行过程中被用户或程序做出的修改是应该保存下来的,这样程序 ...
随机推荐
- Eclipse导入servlet项目报错
Eclipse导入servlet项目,缺少servlet的jar包,导致项目报错. 解决: step1:选中项目->properties step2:选择的Targeted Runtimes s ...
- Python之Suds库调用WCF实现复杂参数序列化
今年主要做自动化测技术支持工作,最近一直在做接口自动化这块,前些天在研究将web页面模拟http进行接口自动化,这周杭州那边想测试WCF服务,所以这两天一直在探索.遇到的第一个问题就是服务参数传参序列 ...
- linux命令dd
原文链接: http://blog.csdn.net/adaptiver/article/details/6672592 dd 使用dd这个linux命令可以创建一定大小文件. linux创建文件命令 ...
- POJ 3621 Sightseeing Cows [最优比率环]
感觉去年9月的自己好$naive$ http://www.cnblogs.com/candy99/p/5868948.html 现在不也是嘛 裸题,具体看学习笔记 二分答案之后判负环就行了 $dfs$ ...
- python实现时间o(1)的最小栈
这是毕业校招二面时遇到的手写编程题,当时刚刚开始学习python,整个栈写下来也是费了不少时间.毕竟语言只是工具,只要想清楚实现,使用任何语言都能快速的写出来. 何为最小栈?栈最基础的操作是压栈(pu ...
- 【笔记】h5 页面唤起电话呼叫
参考文章:https://www.cnblogs.com/lilin1995/p/5640684.html 最近完成一个公司的官网移动端页面,涉及到了唤起电话这个功能,说实在js 并没有为此提供 ap ...
- Websocket原理及使用场景[转载]
WebSocket的使用场景 社交聊天.弹幕.多玩家游戏.协同编辑.股票基金实时报价.体育实况更新.视频会议/聊天.基于位置的应用.在线教育.智能家居等需要高实时的场景 由轮询到WebSocket 1 ...
- Eclipse Java,debug模式无法调试,调试按钮不可用时解决办法
经常出现debug模式进入后,debug的几个按钮置灰,F5,6,8,没有任何反应时,这样操作: 退出 Eclipse.打开 Eclipse 目录下的 configuration 下的 org.ecl ...
- laravel中实现短信发送验证码
前段时间想实现一个短信验证码的功能,但是卡了很长时间. 首先我用的是阿里云的短信服务业务,其首次接入流程如下: 在阿里云上开通短信服务后需要做的: 1,申请签名 2,申请模板 3,创建Acces ...
- JaveScript函数(JS知识点归纳六)
1.函数的基本使用 a)作用:代码的复用,灵活性比较强 b)声明方式:function 名 (形参){函数体} c)调用: 名(实参); d)封装函数--书写一个函数的结构,而且放入一些功能,在需要使 ...