很长时间没用Oracle数据库了,今天在公司的电脑上装了一个Oracle11g,安装完成后,顺便写了个简单的Jdbc连接Oracle的例子,现在记录一下,方便以后查看:
例子很简单,直接上代码:
(注意:在测试之前要先把Oracle的驱动jar放到你建立的工程的lib下)
一:JdbcUtil.java
package cn.gov.csrc.base.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcUtils {
private static final String driverUrl = "oracle.jdbc.driver.OracleDriver";
private static final String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
private static final String username = "CSRC";
private static final String password = "CSRC";
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName(driverUrl);
connection = DriverManager.getConnection(url, username, password);
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
// 测试Oracle连接是否成功
public static void main(String[] args) {
Connection connection = JdbcUtils.getConnection();
System.out.println("连接成功:"+connection);
}
}
连接成功:oracle.jdbc.driver.T4CConnection@481adc30