当前位置: 首页 > news >正文

Connect to Oracle Database with JDBC Driver

1. Overview

The Oracle Database is one of the most popular relational databases. In this tutorial, we’ll learn how to connect to an Oracle Database using a JDBC Driver.

2. The Database

To get us started, we need a database. If we don’t have access to one, let’s either download and install a free version from Oracle Database Software Downloads or use one of the docker images found at Oracle Database Container Images.

For this article, let’s build and run a docker image ofOracle Database 23ai (23.5.0).

3. Maven Setup

Now that we have a database let’s add the required dependency to our project for Oracle’s JDBC driver. We’ll use ojdbc11 to connect to Oracle 23ai:

<dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc11</artifactId> <version>23.5.0.24.07</version> </dependency>Copy

The most recent version of ojdbc11 can be found in the Central Maven Repository. This dependency requires Java 11 or later and is the recommended driver for more recent versions of Java.

For legacy support, ojdbc8 is available for Java 8. Oracle also recommends ojdbc10 as the driver for Oracle 19c.

4. Connect to Oracle Database

To connect to the database, let’s create anOracleDataSource, Oracle’s implementation of theDataSourceinterface. This is preferable to usingDriverManagersinceDataSourceis more scalable and easier to set up.

First, let’s initialize the connection properties and set the properties and the URL in theOracleDataSource. After that, we’ll callgetConnection()to retrieve a new connection:

public static Connection getConnection(String databaseUrl, String userName, String password) throws SQLException { var connectionProperties = new Properties(); connectionProperties.setProperty(OracleConnection.CONNECTION_PROPERTY_USER_NAME, userName); connectionProperties.setProperty(OracleConnection.CONNECTION_PROPERTY_PASSWORD, password); var oracleDataSource = new OracleDataSource(); oracleDataSource.setConnectionProperties(connectionProperties); oracleDataSource.setURL(databaseUrl); return oracleDataSource.getConnection(); }Copy

It should also be noted thatOracleDataSourcehas the methodssetUser()andsetPassword(),which can be used instead ofsetConnectionProperties(). However, it is helpful to note thatOracleConnectionprovides many property names statically, and this is how we would set other properties such as auto-commit, caching, or fetch sizes.

To test out ourgetConnection()method, let’s retrieve the username:

@Test void whenConnectionRetrieved_thenUserNameIsReturned() throws SQLException { var url = "jdbc:oracle:thin:@//localhost:1521/FREEPDB1"; var userName = "BAELDUNG"; var password = "baeldung_pw"; String retrievedUser = null; try (var connection = ConnectToOracleDb.getConnection(url, userName, password)) { retrievedUser = connection.getMetaData().getUserName(); } assertEquals(userName, retrievedUser); }Copy

This example creates the connection in a try-with-resources block, automatically closing the connection after we’re done.

If any mistake is made in the URL, username, or password, we would expect to see an ORA error. For example, ORA-17868 would indicate an unknown host and ORA-01017 indicates a credentials error.

5. Optimizing Performance With Connection Pooling

There are many things to consider when optimizing performance. In particular, if you are setting up a web application, consider using a connection pool.

A connection pool is a cache of database connections that can be reused.

Oracle provides a Universal Connection Pool (ucp11) for Java 11+ and up and Java 8. This is an additional dependency on top of the JDBC dependency, but many other libraries, such as Hikari, Tomcat, and Apache Commons DBCP2, are available for connection pooling.

6. Conclusion

As we now know, retrieving a connection to the database does not require much effort.

http://www.jsqmd.com/news/543840/

相关文章:

  • 【JavaEE】Spring Boot 快速上手
  • ARM中断处理流程
  • 【CVPR 2025】HVI低光增强网络架构解析:从颜色空间到交叉注意力机制
  • 联想M920x黑苹果配置指南:从硬件适配到性能优化的完整方案
  • MBPFan:解决MacBook Linux系统散热难题的智能温控工具
  • 稀疏卷积在医学图像分析中的5个实战技巧(附MinkowskiEngine代码)
  • Linux系统性能监控:确保FRCRN语音降噪服务稳定运行
  • 基于三菱PLC和MCGS组态农田智能灌溉系统 我们主要的后发送的产品有,带解释的梯形图接线图原...
  • 在 SAP 中,应收票据的收取、到期承兑、贴现均有标准操作路径与事务码,以下按业务场景给出操作步骤、T-code、会计分录(含附 / 不附追索权)
  • 保姆级教程:用Cloudreve+Obsidian打造私人云笔记(附WebDAV配置避坑指南)
  • 别再乱传参数了!手把手教你调试uniapp抖音小程序的getPhoneNumber接口
  • HoRain云--CMake高级特性完全指南
  • HunyuanVideo-Foley惊艳效果展示:城市街道环境音效+动态视频同步生成作品集
  • 麒麟系统桌面右下角时间卡顿?别急着重启,先查查mate-indicators这个‘内存刺客’
  • 手把手教你用TwinCAT3和Matlab 2019b配置松下A6伺服(EtherCAT通讯避坑指南)
  • OpenClaw+GLM-4.7-Flash内容创作:自动生成技术文档与博客
  • VScode Verilog辅助开发插件 VScode SystemVerilog辅助开发插件
  • 5步掌握Meshroom革新性3D重建技术:从图像到模型的全流程指南
  • Ubuntu 20.04 Auditd实战:如何优雅地解析用户命令日志(附ausearch技巧)
  • PyTorch 3.0静态图分布式训练全链路剖析:从FX Graph捕获、Dynamo后端注册到自定义DeviceMesh编译优化的6层技术栈解密
  • NumPy:数组元素修改
  • 内网明明通了,外网却“一顿一顿”?手撕动态NAT,真相让人恍然大悟
  • Wan2.2-I2V-A14B惊艳案例:‘量子波动撕裂时空’科幻感特效视频生成
  • 告别论文熬夜焦虑:Paperxie AI 毕业论文写作,让初稿生成不再是噩梦
  • 解放双手!部署这套AI数字员工源码系统,让AI替你写代码、回邮件、做报表
  • 【带AI】基于SpringBoot+Vue3的仓库库存管理系统设计与实现+万字文档+指导搭建视频
  • OpCore-Simplify:零代码3步完成黑苹果EFI配置的终极指南
  • BiliTools哔哩哔哩工具箱完整指南:5个实用技巧高效下载B站资源
  • Pixel Dimension Fissioner 实时生成挑战与优化:WebSocket流式传输方案
  • OpenClaw低配优化:在4GB内存运行Qwen3.5-4B-Claude