各个数据库 jdbc 连接信息

oracle:

数据库连接信息:

##数据库配置##
jdbc.driver = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@{ip}:{port}/{dbName}
jdbc.user = {}
jdbc.password = {}

驱动:

<dependency>
    <groupId>com.oraclegroupId>
    <artifactId>ojdbc6artifactId>
    <version>1.0version>
dependency>

SQL Server:

数据库连接信息:

jdbc.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url = jdbc:sqlserver://{ip}:{port};databasename={}
jdbc.username = {}
jdbc.password = {}

驱动:

<dependency>
    <groupId>com.microsoft.sqlservergroupId>
    <artifactId>sqljdbc4artifactId>
    <version>4.0version>
dependency>

Mysql:

数据库连接信息:

jdbc.driverClassName =com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://{ip}:{port}/{dbName}
jdbc.user = {}
jdbc.pass = {}

驱动:

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>5.1.6version>
dependency>

常用的数据库连接池(数据源):
参考:http://blog.csdn.net/yangyz_love/article/details/8199207
参考:spring-framework-reference.pdf 19.3 Controlling database connections

DriverManagerDataSource:

说明:DriverManagerDataSource建立连接是只要有连接就新建一个connection,根本没有连接池的作用。

id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>


property-placeholder location="jdbc.properties"/>

DBCP configuration:

id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>


property-placeholder location="jdbc.properties"/>

C3P0 configuration:

id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>


property-placeholder location="jdbc.properties"/>

jdbc连接数据库:

package util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 * 测试数据库连接是否成功
 * 
 * @author zz
 * @data 2017年9月13日 上午10:18:56
 *
 */
public class DBConnection {

    private static Logger log = Logger.getLogger(DBConnection.class);

    private DBConnection() {

    } 

    public static Connection getConnection(String name) {
        try {
            Properties prop = new Properties();
            prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(name));

            String driver = prop.getProperty("jdbc.driverClassName");
            String url = prop.getProperty("jdbc.url");
            String user = prop.getProperty("jdbc.username");
            String password = prop.getProperty("jdbc.password");

            log.debug("driver: " + driver);
            log.debug("url: " + url);
            log.debug("user: " + user);
            log.debug("password: " + password);

            Connection conn = null;
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
            return conn;
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage());
        } catch (SQLException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return null;
    }

    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
                log.error(e.getMessage());
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                log.error(e.getMessage());
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                log.error(e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        DBConnection.getConnection("jdbc.properties");
    }
}

你可能感兴趣的:(数据库)