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连接成功");}
}
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步