java得到數據庫鏈接(硬編碼)

package cn.ztfgz.inas.gr.test;

import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JavaGetDatabaseConnection {
    @Test
    //java得到mysql數據庫鏈接
    public void getMysqlConnection(){
        try {
            String driverClassName = "com.mysql.jdbc.Driver";//mysql數據庫驅動
            String url = "jdbc:mysql://localhost:3306/springboot";//mysql數據庫url,springboot爲數據庫名
            String username = "root";//mysql數據庫用戶名
            String password = "1234";//mysql數據庫密碼
            Class.forName(driverClassName);//註冊mysql數據庫驅動
            Connection connection= DriverManager.getConnection(url, username, password);//經過url,username,password得到數據庫鏈接
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Test
    //java得到oracle數據庫鏈接
    public void getOracleConnection(){
        try {
            String driverClassName = "oracle.jdbc.OracleDriver";//oracle數據庫驅動
            String url = "jdbc:oracle:thin:@135.224.183.66:1522:smart102";//oracle數據庫url,smart102爲數據庫名
            String username = "ossl";//oracle數據庫用戶名
            String password = "ossl";//oracle數據庫密碼
            Class.forName(driverClassName);//註冊oracle數據庫驅動
            Connection connection = DriverManager.getConnection(url, username, password);
            System.out.println(connection);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
相關文章
相關標籤/搜索