溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

JDBC 連接數(shù)據(jù)庫(kù)

發(fā)布時(shí)間:2020-08-20 05:48:55 來(lái)源:網(wǎng)絡(luò) 閱讀:421 作者:戀上程序員 欄目:數(shù)據(jù)庫(kù)

一、通過(guò)Driver接口直接連接

/**
 * 通過(guò)Driver獲取Connection
 * @return
 */
public Connection getConnectionByDriver() throws Exception{
    String driverClass = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql:///hdz";
    String user = "root";
    String password = "123456";
    Driver driver = new com.mysql.jdbc.Driver();
    Properties info = new Properties();
    info.setProperty("driverClass", driverClass);
    info.setProperty("user", user);
    info.setProperty("password", password);
    Connection connection = driver.connect(url, info);
    return connection;
}

二、通過(guò)DriverManager直接連接

/**
 * 通過(guò)DriverManager獲取Connection 
 * @return
 * @throws Exception
 */
public Connection getConnectionByDriverManager() throws Exception{
    String url = "jdbc:mysql:///hdz";
    String user = "root";
    String password = "123456";
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(url, user, password);
    return connection;
}
/**
    * 通過(guò)把參數(shù)寫(xiě)在配置文件的方式獲取Connection
    * @return
    * @throws Exception
*/
public Connection getConectionByProperties() throws Exception{
	InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
	Properties info = new Properties();
	info.load(inputStream);
	String url = info.getProperty("url");
	Class.forName("com.mysql.jdbc.Driver");
	Connection connection = DriverManager.getConnection(url, info);
	return connection;
}
url=jdbc:mysql:///hdz
user=root
password=123456

三、通過(guò)DBCP數(shù)據(jù)源連接

@Test
public void testDbcp() throws Exception {
    final BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    basicDataSource.setUrl("jdbc:mysql:///hdz");
    basicDataSource.setUsername("root");
    basicDataSource.setPassword("123456");
    basicDataSource.setInitialSize(2);
    basicDataSource.setMaxActive(2);
    basicDataSource.setMinIdle(2);
    basicDataSource.setMaxWait(2000);
    Connection connection1 = basicDataSource.getConnection();
    System.out.println(connection1);
    Connection connection2 = basicDataSource.getConnection();
    System.out.println(connection2);
    new Thread(){
        @Override
        public void run() {
            Connection connection3;
            try {
                connection3 = basicDataSource.getConnection();
                System.out.println(connection3);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }.start();
    Thread.sleep(3000);
    connection2.close();
}

或者通過(guò)配置文件,BasicDatasourceFactory工廠方式

private DbcpDataSource() {
    Properties info = new Properties();
    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");
    try {
        info.load(inputStream);
        dataSource = BasicDataSourceFactory.createDataSource(info);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
dbcp.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///hdz
username=root
password=123456

initialSize=5
maxActive=10
minIdle=5
maxWait=5000

四、通過(guò)C3P0數(shù)據(jù)源連接

public class C3p0DataSourceUtils {
private DataSource dataSource = null;
private static C3p0DataSourceUtils instance = new C3p0DataSourceUtils();
private C3p0DataSourceUtils(){
    dataSource = new ComboPooledDataSource("intergalactoApp");
}
public static C3p0DataSourceUtils newInstance(){
    return instance;
}
public Connection getConnection() {
    try {
        return dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
    }
}
c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
  <!-- This app is massive! -->
  <named-config name="intergalactoApp"> 
    <property name="acquireIncrement">1</property>
    <property name="initialPoolSize">1</property>
    <property name="minPoolSize">1</property>
    <property name="maxPoolSize">1</property>
    <property name="maxIdleTimeExcessConnections">1000</property>
    <!-- intergalactoApp adopts a different approach to configuring statement caching -->
    <property name="maxStatements">10</property> 
    <property name="maxStatementsPerConnection">5</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///hdz</property>
<property name="user">root</property>
<property name="password">123456</property>
  </named-config>
</c3p0-config>


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI