ORACLE 数据库连接
操作步骤:(oracle 数据库的连接)
1、创建一个项目
2、导入操作 Oracle 的驱动包,驱动包的下载本质是一个.class 文件,包的加载路径
就是 classpath。
(1)导包的基本操作:builderpath--->Congure Builder Path--->Add
External JARs-->找到包的路径-->ojdbc6.jar.
3、创建数据连接对象的工具类
(1)准备连接数据库的素材(数据库的用户名,数据库的密码,数据库的连接地址,
驱动的名)<范例在>
(2)加载驱动(利用反射的方式加载驱动)
使用静态代码块来加载驱动
Class.froName(驱动的全名称)
(4)取得数据库的连接,把数据库保存在在本地的线程里面 ThreadLocal
(5)创建本地的线程的对象,ThreadLocal thread_local = new
ThreadLocal();
thread_local.get():取得线程初始化的值
thread_local.set():保存线程初始化的值
(6)关闭数据库的连接
取得本地线程的值,判断是否为空和是否关闭,没有关闭则关闭
范例:创建数据库的连接
public class DatabaseConnection {
// 取得连接数据库的素材
private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String USER = "SCOTT";
private static final String PASSWORD = "TIGER";
private static ThreadLocal<Connection> thread_local = new ThreadLocal<Connection>();
// 加载数据库的驱动包
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 取得数据库的连接
public static Connection getConnection() {
Connection conn = thread_local.get();
if (conn == null) {
评论0
最新资源