Java连接Oracle有两种方式,一种是JDBC,另一种是JDBC-ODBC桥接模式,后者基本上很少用,甚至在JDK1.8中都没有后者的驱动了,这里主要是前者。举例说明:创建一个Java Project。
创建帮助类SQLHelper.java:
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class SQLHelper {
// 定义三个变量
private static Connection ct = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
// 连接数据库的用户名,密码,url驱动
// 实际开发中,把这些变量写到外部文件中
// 当程序启动时候,我们读入这些配置信息 java.util.Properites
private static String username = null;
private static String password = null;
private static String driver = null;
private static String url = null;
// 类实例化之前就执行,即编译就执行了
// 使用静态块加载驱动
static{
try {
// 使用Properties类,来读取配置文件
Properties pp = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream("dbinfo.properties");
pp.load(fis);
// 取出username,password,driver,url
username = (String) pp.get("username");
password = (String) pp.get("password");
driver = (String) pp.get("driver");
url = (String) pp.get("url");
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 统一的cud操作
public static void executeUpdate(String sql,String[] parameters){
try {
ct = DriverManager.getConnection(url);
ps = ct.prepareStatement(sql);
if (parameters != null) {
for (int i = 0; i < parameters.length; i++) {
ps.setString(i + 1, parameters[i]);
}
ps.executeQuery();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
close(rs, ps, ct);
}
}
//完成查询任务
// sql 表示要执行的sql语句
public static ResultSet executeQuey(String sql,String[] parameters){
//对SQL语句赋值
try {
// 得到连接
ct = DriverManager.getConnection(url, username, password);
// 创建ps对象 SQL语句对象
ps = ct.prepareStatement(sql);
// 如果paramters不为null,我们就去赋值
if (parameters != null) {
for (int i = 0; i < parameters.length; i++) {
ps.setString(i+1, parameters[i]);
}
}
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally{
//close(rs,ps,ct);此处不能关闭
}
return rs;
}
// 关闭资源
public static void close(ResultSet rs,Statement ps,Connection ct){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static Connection getCt() {
return ct;
}
public static PreparedStatement getPs() {
return ps;
}
}
测试类Test:
import java.sql.*;
public class Test{
// 此程序演示对oracle的Query操作
public static void main(String[] args) {
sel();
}
public static void sel() {
String sql = "select * from emp";
ResultSet rs = SQLHelper.executeQuey(sql, null);
try {
while(rs.next()){
System.out.println(rs.getString("ename"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
SQLHelper.close(rs, SQLHelper.getPs(), SQLHelper.getCt());
}
}
}
数据库信息dbinfo.properties:
username = scott
password = tiger
driver = oracle.jdbc.driver.OracleDriver
url =jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl