/**
* 了解: 利用 Driver 接口的 connect 方法获取连接
*/
// 第一种实现
/**
* 了解: 利用 Driver 接口的 connect 方法获取连接
*/
@Test
public void oracleJdbcTest() throws Exception {
Driver driver = null; // sun提供的接口
String url = "jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
Properties info = null;
info = new Properties();
info.put("user", "scott");
info.put("password", "tiger");
driver = new OracleDriver(); // Oracle数据库厂商自己实现sun提供的接口Driver
Connection connect = driver.connect(url, info); // 获取连接
System.out.println(connect);
}
======================================
/**
* 了解: 使用 DriverManager 来获取数据库连接
* 版本1:
* 好处: 不需要使用原生的 Driver 方法来获取连接.
* 缺点: 还是耦合了具体的实现类.
*/
//第二种实现
@Test
public void oracleJdbcTest1() throws Exception{
Connection connection=null;
DriverManager.registerDriver(new OracleDriver()); //驱动管理器注册Oracle驱动,实现Java程序可以连接Oracle数据库,如果想要连接不同的数据库则需要注册不同的数据库驱动
String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
Properties info=null;
info=new Properties();
info.put("user", "scott");
info.put("password", "tiger");
connection=DriverManager.getConnection( url, info) ; //DriverManager驱动管理器类,里面的方法都是静态的,类调用获取到一个连接
System.out.println(connection);
}
====================================
/**
* 了解: 更进一步, 不需要关联任何 JDBC 驱动的实现类。
* 但需要提供 JDBC 驱动中 Driver 接口的实现类的全类名的字符串.
*/
//第三种实现
@Test
public void oracleJdbcTest2() throws Exception, InstantiationException, IllegalAccessException, ClassNotFoundException{
Connection connection=null;
String className="oracle.jdbc.driver.OracleDriver";
DriverManager.registerDriver((Driver)Class.forName(className).newInstance()); //注册驱动
String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
Properties info=null;
info=new Properties();
info.put("user", "scott");
info.put("password", "tiger");
connection=DriverManager.getConnection(url,info);
System.out.println(connection);
}
===========================================================
/**
* 能创建一个不和具体 Driver 耦合的获取数据库连接的方法. 即在方法中不再关联任何数据库驱动的 JDBC 实现.
* @throws SQLException
*/ /**
* final version: 若需要手动获取数据库连接:
*
* 更进一步, 不需要关联任何 JDBC 驱动的实现类。
* 但需要提供 JDBC 驱动中 Driver 接口的实现类的全类名的字符串.
*
* 实际上, 在驱动的实现类中有一个静态代码块: 创建了 Driver 实现类的对象, 并把其注册给 DriverManager
* static {
* try {
* java.sql.DriverManager.registerDriver(new Driver());
* } catch (SQLException E) {
* throw new RuntimeException("Can't register driver!");
* }
* }
*
* 而调用 Class 的 forName 方法在加载类实例时, 会调用静态代码块.
*
*/
//第四种实现(最常用)
@Test
public void oracleJdbcTest3() throws Exception{
Connection connection=null;
String className="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
Properties info=null;
info=new Properties();
info.put("user", "scott");
info.put("password", "tiger");
Class.forName(className).newInstance();//加载驱动
connection=DriverManager.getConnection(url,info);
System.out.println(connection);
}

最新文章

  1. 机器学习中的算法(1)-决策树模型组合之随机森林与GBDT
  2. FIR.im Weekly - 这是多产的一周
  3. Node.js 的module 系统
  4. Java里面instanceof怎么实现的
  5. 冒泡排序最佳情况的时间复杂度,为什么是O(n)
  6. js格式化时间戳
  7. Mac OSX Sierra WiFi connecting problem
  8. shell初步了解
  9. 【java】网络socket编程简单示例
  10. centos7之rsync+serrsync
  11. ubuntun与qt下载地址
  12. laravel 微信小程序登录 加密解密扩展包
  13. SSB基准测试
  14. JAVA获取运行环境的信息
  15. java_sql_Batch_批处理
  16. 2017-2018-1 JaWorld 团队作业--冲刺5
  17. 串行通讯协议--起止式异步通讯协议(UART)
  18. .net托管资源与非托管资源
  19. java.sql.SQLException: Incorrect string value: '\xF0\x9F\x9A\x80\xF0\x9F...' for column 'name' at row 1
  20. try catch finally的用法

热门文章

  1. springboot整合solr
  2. [WPF自定义控件库] 给WPF一个HyperlinkButton
  3. 40 篇原创干货,带你进入 Spring Boot 殿堂!
  4. 使用docker快速搭建本地环境
  5. hibernate.validator 与 jackson
  6. PrintWriter out = response.getWriter();乱码解决
  7. deepin 15.11 安装 pyenv
  8. Redis学习总结(二)--Redis数据结构
  9. #第 12 篇:解锁博客侧栏,GoGoGo!
  10. Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)