开发中使用properties配置文件,方便后期维护。

  1. 文件位置: 任意,建议src下
  2. 文件名称:任意,扩展名为properties
  3. 文件内容:一行一组数据,格式“key=value”  
    1. key 命名自定义,如果是多单词,习惯使用点分割,例如jdbc.driver
    2. 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. 1、javaweb学习之配置文件web.xml

    今天这里主要讲述javaweb中的配置文件web.xml中的内容及其作用,都是基础部分,对于初学者需要好好掌握理解. 简单配置: <servlet>    <servlet-name ...

  2. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  3. 在JavaWeb中使用Log4j步骤

    在JavaWeb中使用Log4J指南.每次在开始写一个项目的时候都忘记Log4J如何配置.所以写个步骤,作为记录. 第一步 下载Log4J jar包 从Apache Logging Services ...

  4. 在javaweb中通过servlet类和普通类读取资源文件

    javaweb有两种方式读取资源文件 在Servlet中读取,可以使用servletContext,servletContext可以拿到web所有的资源文件,然后随便读,但是这种方法不常用,尽量少在S ...

  5. JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用

    JavaWeb中监听器+过滤器+拦截器区别.配置和实际应用 1.前沿上一篇文章提到在web.xml中各个元素的执行顺序是这样的,context-param-->listener-->fil ...

  6. JavaWeb中的监听器

    JavaWeb中的监听器 l  事件源:三大域! ServletContext ¨       生命周期监听:ServletContextListener,它有两个方法,一个在出生时调用,一个在死亡时 ...

  7. 在开发中关于javaweb中的路径问题小结

    转自http://blog.csdn.net/yinyuehepijiu/article/details/9136117 在javaweb项目中添加配置文件,满足连接数据库配置参数以及其他自定义参数存 ...

  8. javaWEB中的四种域对象

    javaWEB中的四种域对象 (1)ServletContext ServletContext是最大的Web域对象,在整个工程内有效,可以存储一些需要全局部署的配置文件,也可以存储其他信息,不过因为它 ...

  9. C#开发中使用配置文件对象简化配置的本地保存

    C#开发中使用配置文件对象简化配置的本地保存 0x00 起因 程序的核心是数据和逻辑,开发过程中免不了要对操作的数据进行设置,而有些数据在程序执行过程中被用户或程序做出的修改是应该保存下来的,这样程序 ...

随机推荐

  1. win7 重装 docker 启动后无法启动错误解决

    描述 win7 重新安装Docker 后启动  Docker Quickstart Terminal 出现如下错误 Starting "default"... (default) ...

  2. verilog实验2:基于FPGA的59秒计时器设计

    一.实验任务 利用四个数码管显示59秒计时器. 二.代码实现 将开发板的48M晶振分频出1M,然后计数器累加,将计数器结果显示在数码管上.低位逢十进一,第二位逢五进一,依次构成59秒计时器. 部分代码 ...

  3. Java数据结构和算法(九)——高级排序

    春晚好看吗?不存在的!!! 在Java数据结构和算法(三)——冒泡.选择.插入排序算法中我们介绍了三种简单的排序算法,它们的时间复杂度大O表示法都是O(N2),如果数据量少,我们还能忍受,但是数据量大 ...

  4. Go基础之--排序和查找操作

    排序操作主要都在sort包中,导入就可以使用了import("sort") 常用的操作 sort.Ints:对整数进行排序sort.Strings:对字符串进行排序sort.Flo ...

  5. 阿里云学习之API网关

    注:此处仅供api的创建做一个补充参考,API网关的优缺点及创建过程中的参数详情,请参考阿里云开放文档:https://helpcdn.aliyun.com/document_detail/29478 ...

  6. MySQL中的内置系统函数

    一.字符串函数  1. CONCAT(S1,S2....SN): 把传入的参数连接成一个字符串  2. INSERT(str, x, y, insert): 将字符串 X位置开始,y个字符串长度替换为 ...

  7. iperf命令

    iperf命令网络测试 iperf命令是一个网络性能测试工具.iperf可以测试TCP和UDP带宽质量.iperf可以测量最大TCP带宽,具有多种参数和UDP特性.iperf可以报告带宽,延迟抖动和数 ...

  8. Navi.Soft31.产品.微信聊天(永久免费)

    1系统简介 1.1功能简述 微信确实是一款优秀的社交的软件,被越来越多的人使用.它的电脑版最新版本是2.6,更新也比较及时,只是它有一个功能差强人意,就是同一台电脑只能运行一个微信号,不知道为何这样设 ...

  9. .net 分割字符串

    string a = "1-2-3-4-5-6-7-8-9"; string[] b = a.Split(new Char[] { '-' }); for (int i = 0; ...

  10. Hibernate学习(五)lazy属性学习(true和extra区别)

    Lazy(懒加载)在hibernate何处使用:1.<class>标签上,可以取值:true/false,(默认值是:true)2.<property>标签上,可以取值:tru ...