🚀 Master Java database programming, JDBC DataSource, and Oracle DB!
🔔 Subscribe to Ram N Java on YouTubeWhy Use JDBC DataSource Instead of DriverManager?
In modern Java enterprise applications, the standard javax.sql.DataSource interface is preferred over DriverManager. DataSource provides complete abstraction of database connection details, enables connection pooling, and integrates smoothly with JNDI and enterprise application servers.
For Oracle databases, Oracle provides its own implementation class called OracleDataSource (located in the oracle.jdbc.pool package inside ojdbc.jar), allowing you to configure connection properties using clean getter and setter methods.
Database Table Structure (HR.EMPLOYEES)
This demonstration queries the standard EMPLOYEES table inside Oracle's HR sample schema:
SELECT employee_id, first_name, last_name, email, phone_number
FROM employees;
Complete Java Source Code Example
The following Java program configures an OracleDataSource instance, retrieves a connection, and reads employee records:
package com.ram.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import oracle.jdbc.pool.OracleDataSource;
public class JDBCDataSourceDemo {
static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";
static final String USER = "hr";
static final String PASS = "hr";
// Factory method to configure OracleDataSource
public static DataSource getOracleDataSource() throws SQLException {
OracleDataSource oracleDS = new OracleDataSource();
oracleDS.setURL(DB_URL);
oracleDS.setUser(USER);
oracleDS.setPassword(PASS);
return oracleDS;
}
public static void main(String[] args) {
JDBCDataSourceDemo demo = new JDBCDataSourceDemo();
demo.getEmployeeInformation();
}
public void getEmployeeInformation() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Step 1: Obtain the DataSource instance
DataSource dataSource = getOracleDataSource();
// Step 2: Get Connection from DataSource
conn = dataSource.getConnection();
// Step 3: Create Statement and execute SQL query
stmt = conn.createStatement();
String sql = "SELECT employee_id, first_name, last_name, email, phone_number FROM employees";
rs = stmt.executeQuery(sql);
// Step 4: Process the ResultSet
System.out.println("ID | Name | Email | Phone");
System.out.println("----------------------------------------");
while (rs.next()) {
int empId = rs.getInt("employee_id");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
String email = rs.getString("email");
String phone = rs.getString("phone_number");
System.out.println(empId + " | " + firstName + " " + lastName + " | " + email + " | " + phone);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Step 5: Clean up database resources safely
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
Key Steps & Best Practices
-
Configure OracleDataSource: Instantiate
OracleDataSourceand configure it withsetURL(),setUser(), andsetPassword(). -
Program to Interfaces: Return
javax.sql.DataSourceso your application code is decoupled from vendor-specific classes. -
Connection Acquisition: Simply invoke
dataSource.getConnection()without needing explicit driver class loading calls. -
Resource Cleanup: Always close the
ResultSet,Statement, andConnectioninside afinallyblock to prevent database resource leaks.
Related Tutorials You Might Enjoy
Click here to watch in Youtube : https://www.youtube.com/watch?v=I3BRId1c5zY
Click the below Image to Enlarge
![]() |
| JDBC DataSource [Oracle DB] |
![]() |
| JDBC DataSource [Oracle DB] |
![]() |
| JDBC DataSource [Oracle DB] |
![]() |
| JDBC DataSource [Oracle DB] |




No comments:
Post a Comment