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读取配置文件连接,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。