Spring或者JDBC读取配置文件连接

一。JDBC连接类

package com.xwt.dao.impl;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.xwt.dao.DriverDao;

public class DriverDaoSupport implements DriverDao {
public final static ResourceBundle bundle = ResourceBundle.getBundle("DBConnectionConfig");// DBConnectionConfig.properties配置文件的名称
public final static String DRIVER = bundle.getString("mysql.database.driver");
public final static String URL = bundle.getString("mysql.database.url");
public final static String DBNAME = bundle.getString("mysql.database.user");
public final static String DBPASS = bundle.getString("mysql.database.password");

public Connection getConnection() {
 Connection conn = null;
 try {
  Class.forName(DRIVER);
  conn = (Connection) DriverManager
    .getConnection(URL, DBNAME, DBPASS);
 } catch (ClassNotFoundException e) {
  e.printStackTrace();
 } catch (SQLException e) {
  e.printStackTrace();
 }
 return conn;
}

public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
 if (rs != null) {
  try {
   rs.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 if (pstmt != null) {
  try {
   pstmt.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

 if (conn != null) {
  try {
   conn.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

}
}


二。Spring连接

<!--配置文件导入 -->
<bean id="propertyConfigurer"
 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location">
  <value>classpath:DBConnectionConfig.properties</value>
 </property>
</bean>

<!--数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="${mysql.database.driver}" />
 <property name="url" value="${mysql.database.url}" />
 <property name="username" value="${mysql.database.user}" />
 <property name="password" value="${mysql.database.password}" />
 <property name="maxActive" value="${mysql.database.maxActive}" />
 <property name="maxIdle" value="${mysql.database.maxIdle}" />
 <property name="maxWait" value="${mysql.database.maxWait}" />
</bean>


三。DBConnectionConfig.properties配置文件

#mysql
mysql.database.driver=com.mysql.jdbc.Driver
mysql.database.url=jdbc\:mysql\://127.0.0.1\:3306/51didi?useUnicode\=true&amp;amp;characterEncoding\=utf-8
mysql.database.user=root
mysql.database.password=123456
mysql.database.maxActive=100
mysql.database.maxIdle=30
mysql.database.maxWait=500

#oracle
oracle.database.driver=oracle.jdbc.driver.OracleDriver
oracle.database.url=jdbc\:oracle\:thin\:@localhost\:1521\:51didi
oracle.database.user=root
oracle.database.password=123456


Spring或者JDBC读取配置文件连接,布布扣,bubuko.com

Spring或者JDBC读取配置文件连接

上一篇:Mysql-5.5.33主从复制,半同步复制,以及基于SSL的复制


下一篇:Oracle 使用连接显示多个表中的数据