jdbc连接Oracle数据库

导入的jar包:ojdbc14.jar 

注:如果没有导入ojdbc.jar在运行后会报:java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

代码如下:

package com.test.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class DBConnection {

private static final String DRIVER="oracle.jdbc.driver.OracleDriver";

private static final String URL="jdbc:oracle:thin:@127.0.0.1:1521:testdb";

private String name="username";//访问数据库的用户名

private String pwd="password";//访问数据库的密码

Connection conn=null;

public DBConnection(){

try {

Class.forName(DRIVER);

conn = DriverManager.getConnection(URL,name,pwd);

System.out.println("连接成功!");

} catch (Exception e) {

e.printStackTrace();

}

}

public Connection getConn(){

return conn;

}

public void closeConn(){

if(conn!=null){

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) {

new DBConnection();

}

}

你可能感兴趣的:(jdbc连接Oracle数据库)