Retrive Records through PreparedStatement using Oracle JDBC Pool Resource

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 SQLException("Error establishing connection!");
}
String query = "SELECT * FROM users";
PreparedStatement statement = CN.prepareStatement(query);
ResultSet RS = statement.executeQuery();
while (RS.next())
{
out.print(rs.getString("user_name") + "
");
}
%>


Result



Comments

Post a Comment

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