- 先找到Oracle的驱动,在D:/oracle/ora92/jdbc/lib下,有clesses12.jar包,这个就是Oracle的JDBC驱动了
- 配置好Log4j等后,写类如下:
OperDB类:
package com.xianxinzhi.base;
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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author neitbe
*/
public class OperDB extends BaseConnect {
private static Log log = LogFactory.getLog(OperDB.class);
//驱动名称
private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
//Oracle里的表空间
private final String oracleUrlToConnect = "jdbc:oracle:thin:@192.168.26.160:1521:xuxin";
private final String user = "scott";
private final String password = "tiger";
private Connection connection = null;
private PreparedStatement ps = null;
private Statement statement = null;
public OperDB() {
try {
log.debug("准备加载Oracle驱动……");
Class.forName(oracleDriverName);
log.debug("加载驱动成功!");
} catch (ClassNotFoundException e) {
log.error("加载驱动失败,类没有找到",e);
}
}
public PreparedStatement getPs(String sql) {
try {
log.debug("准备得到PreparedStatement……");
ps = this.getConnection().prepareStatement(sql);
log.debug("成功得到PreparedStatement");
} catch (SQLException e) {
log.error("得到PreparedStatement错误",e);
}
return ps;
}
public Statement getStatement() {
try {
log.debug("准备得到statement……");
statement = this.getConnection().createStatement();
log.debug("成功得到statement!");
} catch (SQLException e) {
log.error("得到statement错误",e);
}
return statement;
}
private Connection getConnection() {
try {
log.debug("准备得到连接……");
connection = DriverManager.getConnection(oracleUrlToConnect,user,password);
log.debug("得到连接成功!");
} catch (SQLException e) {
log.error("得到连接失败,SQL异常",e);
}
return connection;
}
public void dbClose(ResultSet rs,PreparedStatement ps) {
try {
log.debug("准备关闭数据库的连接……");
rs.close();
log.debug("ResultSet结果集正常关闭");
ps.close();
log.debug("PreparedStatement正常关闭");
this.connection.close();
log.debug("Connection连接正常关闭");
} catch (SQLException e) {
log.error("关闭数据库错误,SQL异常",e);
}
}
public void dbClose(ResultSet rs,Statement st) {
try {
log.debug("准备关闭数据库的连接……");
rs.close();
log.debug("ResultSet结果集正常关闭");
st.close();
log.debug("PreparedStatement正常关闭");
this.connection.close();
log.debug("Connection连接正常关闭");
} catch (SQLException e) {
log.error("关闭数据库错误,SQL异常",e);
}
}
public ResultSet getResultSetFromPs(PreparedStatement ps) {
ResultSet rs = null;
try {
log.debug("准备从ps直接得到rs……");
rs = ps.executeQuery();
log.debug("从ps得到rs成功!");
} catch (SQLException e) {
log.error("从ps得到rs错误!",e);
}
return rs;
}
}
其中BaseConnect类为空类,以便日后扩展
TestOracle类如下:
package com.xianxinzhi.test;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.xianxinzhi.base.OperDB;
/**
* @author neitbe
*/
public class TestOracle {
private static Log log = LogFactory.getLog(TestOracle.class);
private static String sql = "select * from emp";
public static void main(String args[]) {
OperDB operDB = new OperDB();
PreparedStatement ps = operDB.getPs(sql);
ResultSet rs = operDB.getResultSetFromPs(ps);
try {
while(rs.next()) {
System.out.println();
log.info("第一个字段为:" + rs.getDouble("empno"));
log.info("第二个字段为:" + rs.getString("ename"));
log.info("第三个字段为:" + rs.getString("job"));
log.info("第四个字段为:" + rs.getDate("hiredate"));
log.info("第五个字段为:" + rs.getDouble("sal"));
System.out.println();
}
} catch (SQLException e) {
log.error("数据库错误,SQL异常",e);
} finally {
operDB.dbClose(rs,ps);
log.debug("数据库连接成功关闭!");
}
}
}
运行正常,说明没有客户端可以连接到Oracle的
还有一点要说名:安装完Oracle后,Oracle会把环境变量改掉,这时只能把java虚拟机改回来,不要改bin,否则安装Toad客户端时会出错