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();
        }
    }
}

 

posted @ 2018-09-11 14:49  chenhonggao  阅读(1317)  评论(0编辑  收藏  举报