How to use JDBC Connection Pool Resource in Java Application

In the previous post we created a JDBC Pool Resource and now in this post i will show you that how to use JDBC connection pool resource your Java Application.

1) Import following Classes in your application.

import java.sql.Connection;
import javax.sql.DataSource;
import javax.naming.InitialContext;




2) Create a JDBCconnection Class as mentioned


public class JDBCconnection(){
private DataSource ODS;
private Connection CN;
public JDBCconnection(){
try {
InitialContext ctx = new InitialContext();
/*jdbc/__oracon is a pooled resource name*/
ODS = (javax.sql.DataSource) ctx.lookup("jdbc/__oracon");
CN = ODS.getConnection();
if (CN== null){
System.out.println("Error occur while establishing connection");
}
} catch (Exception e){
System.out.println("Oops! " + e.getMessage());
}
}
}


Now using the Connection CN you can call both PreparedStatement Or CallableStatement to write SQL statement directly in your code or call Stored Function, Procedures etc.

Comments

Popular posts from this blog

What is Oracle Integration Cloud Service - ICS?

How to Create Packages in Oracle Database using TOAD for Oracle

How to create a Simple Scheduler Job in Oracle Database using Toad