Posts

Showing posts with the label PreparedStatement

Retrive Records through PreparedStatement using Oracle JDBC Pool Resource

Image
In this post i will show you to how to select data from database through PreparedStatement using Oracle JDBC Pool Resource. In this example we are selecting all records from Users table and using WHILE LOOP shows all records into browser. 1) Create JSP page using Netbean IDE. 2) Add following classes in your JSP page. <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"% > <%@page import="java.sql.SQLException"%& > <%@page import="java.sql.Connection"% > <%@page import="javax.sql.DataSource"% > <%@page import="javax.naming.InitialContext"%> 3) Add the following code in body tag. This code contain JDBC Pool Resource connection, plain SQL Statement and PreparedStatement. <% InitialContext ctx = new InitialContext(); DataSource ODS = (DataSource) ctx.lookup("jdbc/__oracon"); Connection CN = ODS.getConnection(); if (CN == null) { throw new SQLExcepti...

How to use JDBC Connection Pool Resource in Java Application

Image
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.