本文主要描述通过thin方式连接oracle数据库
1.创建web project ,将D:\oracle\product\10.2.0\db_1\jdbc\lib(oracle安装目录)下的ojdbc14.jar拷贝至项目WEB-INF目录下lib路径中。
2.创建数据库连接文件:
ConnectDB.java:
package com.gree.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectDB {
//定义数据库连接驱动
private final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
//定义数据库连接url
private final String DBURL = "jdbc:oracle:thin:@localhost:1521:orcl";
//定义数据库连接用户名
private final String DBUSER = "180172";
//定义数据库连接密码
private final String DBPSW = "180172";
//定义数据库连接对象
private Connection conn = null;
//错误信息串
private String errMes = null;
//构造方法,加载驱动
public ConnectDB(){
try {
Class.forName(DBDRIVER);
this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPSW);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("加载驱动失败");
errMes = e.toString();
}
}
//取得数据库连接
public Connection getConnection(){
return conn;
}
//关闭数据库连接
public void close(){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭数据库失败");
}
}
//获取错误信息
public String getErrMes()
{
return errMes;
}
//测试数据库连接
public static void main(String []args){
ConnectDB db = new ConnectDB();
System.out.println(db.getConnection());
db.close();
}
}
经验证该文件可连接oracle数据库