Java程序连接数据库
2024-10-06 10:38:34
/**
* 了解: 利用 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)-决策树模型组合之随机森林与GBDT
- FIR.im Weekly - 这是多产的一周
- Node.js 的module 系统
- Java里面instanceof怎么实现的
- 冒泡排序最佳情况的时间复杂度,为什么是O(n)
- js格式化时间戳
- Mac OSX Sierra WiFi connecting problem
- shell初步了解
- 【java】网络socket编程简单示例
- centos7之rsync+serrsync
- ubuntun与qt下载地址
- laravel 微信小程序登录 加密解密扩展包
- SSB基准测试
- JAVA获取运行环境的信息
- java_sql_Batch_批处理
- 2017-2018-1 JaWorld 团队作业--冲刺5
- 串行通讯协议--起止式异步通讯协议(UART)
- .net托管资源与非托管资源
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x9A\x80\xF0\x9F...' for column 'name' at row 1
- try catch finally的用法
热门文章
- springboot整合solr
- [WPF自定义控件库] 给WPF一个HyperlinkButton
- 40 篇原创干货,带你进入 Spring Boot 殿堂!
- 使用docker快速搭建本地环境
- hibernate.validator 与 jackson
- PrintWriter out = response.getWriter();乱码解决
- deepin 15.11 安装 pyenv
- Redis学习总结(二)--Redis数据结构
- #第 12 篇:解锁博客侧栏,GoGoGo!
- Leetcode之深度优先搜索&;回溯专题-980. 不同路径 III(Unique Paths III)