Junit连接oracle数据库

1.package com.oracle.util;

import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil { //连接oracle数据库的应用
    private static final String jdbcName="oracle.jdbc.driver.OracleDriver";
    private static final String url="jdbc:oracle:thin:@localhost:1521:ORAC";
    private static final String user="scott";
    private static final String password="tiger";
    public Connection getConn() throws Exception {
        Class.forName(jdbcName);
      /**
       *DriverManager(唯一一个用到的类)
       *Connection(接口)
       */ Connection conn
=DriverManager.getConnection(url, user, password);                             return conn; } public void closeConn(Connection conn) throws Exception { if(conn!=null){ conn.close(); } } public static void main(String[] args) throws Exception { DbUtil dbUtil=new DbUtil(); Connection conn=dbUtil.getConn(); if(conn!=null){ System.out.println("Oracle数据库连接成功"); }else{ System.out.println("Oracle数据库连接失败"); } dbUtil.closeConn(conn); } }

2.

package com.oracle.test;

import static org.junit.Assert.*;
import java.sql.Connection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.oracle.util.DbUtil;

public class JunitTestOracle { //在Junit中连接oracle数据库 DbUtil dbUtil=new DbUtil(); @Before public void setUp() throws Exception { //setUp()在Junit中用到的方法 } @After public void tearDown() throws Exception { //tearDown()在Junit中用到的方法 } @Test public void test() throws Exception { // 在test()方法中写内容 Connection conn=dbUtil.getConn(); if(conn!=null){System.out.println("Oracle连接成功");} } }
原文地址:https://www.cnblogs.com/lxy151/p/8178042.html