Java_JDBC连接数据库_使用读取配置文件的方式

  1 package com.homewoek3_4.dao;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.sql.Connection;
  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 /**
 13  * 数据库连接通用类
 14  * @author Administrator
 15  */
 16 public abstract class BaseDao {
 17     private static final String path = "database.properties";
 18     private static String DB_DRIVER;
 19     private static String DB_URL;
 20     private static String DB_USER;
 21     private static String DB_PWD;
 22     
 23     protected Connection conn = null;
 24     protected PreparedStatement ps = null;
 25     protected ResultSet rs = null;
 26     
 27     static {
 28         Properties pro = new Properties();
 29         InputStream io = BaseDao.class.getClassLoader().getResourceAsStream(path);
 30         try {
 31             //读取配置文件
 32             pro.load(io);
 33         } catch (IOException e) {
 34             e.printStackTrace();
 35         }
 36         DB_DRIVER = pro.getProperty("DB_DRIVER");
 37         DB_URL = pro.getProperty("DB_URL");
 38         DB_USER = pro.getProperty("DB_USER");
 39         DB_PWD = pro.getProperty("DB_PWD");
 40         try {
 41             //加载驱动类
 42             Class.forName(DB_DRIVER);
 43         } catch (ClassNotFoundException e) {
 44             e.printStackTrace();
 45         }
 46     }
 47     /**
 48      * 打开数据库连接
 49      */
 50     protected void openConn() {
 51         try {
 52             conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
 53         } catch (SQLException e) {
 54             e.printStackTrace();
 55         }
 56     }
 57     /**
 58      * 关闭数据库连接
 59      */
 60     protected void closeConn() {
 61         try {
 62             if (rs != null) {
 63                 rs.close();
 64             }
 65             if (ps != null) {
 66                 ps.close();
 67             }
 68             if (conn != null) {
 69                 conn.close();
 70             }
 71         } catch (SQLException e) {
 72             e.printStackTrace();
 73         }
 74     }
 75     /**
 76      * 增删改
 77      * @param sql
 78      * @param obj
 79      * @return
 80      */
 81     protected int executeUpdate(String sql, Object...obj) {
 82         int result = -1;
 83         this.openConn();
 84         try {
 85             ps = conn.prepareStatement(sql);
 86             if (obj != null) {
 87                 for (int i = 0; i < obj.length; i++) {
 88                     ps.setObject(i+1, obj[i]);
 89                 }
 90             }
 91             result = ps.executeUpdate();
 92         } catch (SQLException e) {
 93             e.printStackTrace();
 94         }
 95         return result;
 96     }
 97     /**
 98      * 查询
 99      * @param sql
100      * @param obj
101      * @return
102      */
103     protected void executeQuery(String sql, Object...obj) {
104         this.openConn();
105         try {
106             ps = conn.prepareStatement(sql);
107             if (obj != null) {
108                 for (int i = 0; i < obj.length; i++) {
109                     ps.setObject(i+1, obj[i]);
110                 }
111             }
112             rs = ps.executeQuery();
113         } catch (SQLException e) {
114             e.printStackTrace();
115         }
116     }
117 }

创建database.properties文件,注意:后缀名一定是properties。此格式的文件在MyEclipse中图标会变,这点很容易写错,要注意。

1 DB_DRIVER=com.mysql.jdbc.Driver
2 DB_URL=jdbc:mysql://localhost:3306/Pet?useUnicode=true&characterEncoding=utf8&useSSL=true
3 DB_USER=root
4 DB_PWD=111111

 

posted @ 2017-08-11 11:44  南橘  阅读(715)  评论(0编辑  收藏  举报
蜘蛛