jdbc连接的工具类

在不实用框架的情况下,有一个jdbc的工具类来进行数据库的连接就再好不过了,下面提供这个工具类DBUtil.java

 

  1 package org.jdbc.test;
  2 
  3 import java.io.InputStream;
  4 import java.sql.Connection;
  5 import java.sql.Driver;
  6 import java.sql.DriverManager;
  7 import java.sql.PreparedStatement;
  8 import java.sql.ResultSet;
  9 import java.sql.SQLException;
 10 import java.util.Properties;
 11 
 12 public class DBUtil {
 13     
 14     
 15     
 16     /*
 17      * 编写一个通用的方法,在不修改程序的情况下,可以获取任何数据库的连接
 18      * 解决方案:把数据库驱动Driver实现类的全类名、url、user、password
 19      * 放入一个配置文件中,通过修改配置文件的方式实现和具体的数据库解耦
 20      */
 21     public static Connection getConnection2() throws Exception{
 22     String driverClass = null;
 23     String jdbcUrl = null;
 24     String user = null;
 25     String password = null;
 26 
 27     // 读取类路径下的jdbc.properties文件
 28     InputStream in =DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
 29     Properties properties = new Properties();
 30     properties.load(in);
 31     driverClass = properties.getProperty("driver");
 32     jdbcUrl = properties.getProperty("jdbcUrl");
 33     user = properties.getProperty("user");
 34     password = properties.getProperty("password");
 35 
 36     Driver driver = (Driver)Class.forName(driverClass).newInstance();
 37 
 38     Properties info = new Properties();
 39     info.put("user", user);
 40     info.put("password", password);
 41 
 42     Connection connection = driver.connect(jdbcUrl, info);
 43     return connection;
 44     }
 45 
 46 
 47     /**
 48      * 获取数据库连接
 49      * 
 50      * @return
 51      */
 52     public static Connection getConnection() {
 53 
 54         try {
 55             String url = "jdbc:mysql://127.0.0.1:3306/crm?user=root&password=root";
 56             // 加载驱动
 57             Class.forName("com.mysql.jdbc.Driver");
 58             // 获取连接
 59             //Connection con = DriverManager.getConnection(url);
 60             Connection con = DriverManager.getConnection(url);
 61             
 62             return con;
 63         } catch (Exception e) {
 64             e.printStackTrace();
 65         }
 66 
 67         return null;
 68     }
 69     
 70     /**
 71      * 关闭连接
 72      * @param con
 73      */
 74     public static void  closeCon(Connection con){
 75         try {
 76             con.close();
 77         } catch (SQLException e) {
 78             // TODO Auto-generated catch block
 79             e.printStackTrace();
 80         }
 81     }
 82     
 83     /**
 84      * 关闭实例
 85      * @param pre
 86      */
 87     public static void  closePre(PreparedStatement pre){
 88         try {
 89             pre.close();
 90         } catch (SQLException e) {
 91             // TODO Auto-generated catch block
 92             e.printStackTrace();
 93         }
 94     }
 95     
 96     /**
 97      * 关闭结果集
 98      * @param res
 99      */
100     public static void  closeRes(ResultSet res){
101         try {
102             res.close();
103         } catch (SQLException e) {
104             // TODO Auto-generated catch block
105             e.printStackTrace();
106         }
107     }
108 
109 
110 
111 }
View Code

该工具类提供了两种读取jdbc连接的方法,一种是写在单独的properties文件中,这种方法再切换数据库链接的时候比较容易,直接找到properties文件进行修改就可以了,

另外一种是比较传统的写法,连接信息直接写在该类中,直接调用,

 

个人感觉写在properties文件中的这种写法比较好

转载于:https://www.cnblogs.com/geekdc/p/5194315.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值