You are on page 1of 4

1)Import the packages:

<%@ page import="java.sql.*"%>

2)Load the oracle driver:

Class.forName("oracle.jdbc.driver.OracleDriver");

3) Connect to database:

a) If you are using oracle oci driver,you have to use:


Connection connection= DriverManager.getConnection("jdbc:oracle:oci8:
@oracle.world", "root", "root");

where oracle.world is the TNS NAMES entry and root is the username and password.

b) If you are using oracle thin driver,you have to use:

Connection connection= DriverManager.getConnection ("jdbc:oracle:thin:@localhost:3306:roseindia",


"root", "root");

where localhost is the host,3306 is the port, roseindia is the database and root is the username and
password.

4)Querying the database:

a)create statement:

Statement st = connection.createStatement();

b)write query and execute the query:

ResultSet rs = st.executeQuery("SELECT * from data");

5) Close the statement,resultset and connection:

rs.close();
st.close();
connection.close();
Example1:

<%@ page import="java.sql.*"%>


<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin: @localhost:3306:Oracle",
"rose", "rose");
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("Select * from data");
while(rs.next(){
String name=rs.getString("name");
String add=rs.getString("address");
out.println(name+" "+add);
}
rs.slose();
st.close();
connection.close();
}
catch (Exception ex) {}
%>

Example2:

<%@ page import="java.sql.*" %>

<HTML>
<HEAD><TITLE>Simple JSP/Oracle Query Example</TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<B>Employees</B>
<BR><BR>

<%
Connection conn = null;
try
{

Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@fuju.exzilla.net:1521:fuju",
"scott",
"tiger");

Statement stmt = conn.createStatement();


ResultSet rs = stmt.executeQuery("SELECT * FROM scott.employees");
//Print start of table and column headers
out.println("<TABLE CELLSPACING=\"0\" CELLPADDING=\"3\" BORDER=\"1\">");
out.println("<TR><TH>ID</TH><TH>NAME</TH><TH>SURNAME</TH>");
out.println(" <TH>SALARY</TH><TH>STARTDATE</TH></TR>");
//Loop through results of query.

while(rs.next())
{
out.println("<TR>");
out.println(" <TD>" + rs.getString("EMPID") + "</TD>");
out.println(" <TD>" + rs.getString("ENAME") + "</TD>");
out.println(" <TD>" + rs.getString("ESURNAME") + "</TD>");
out.println(" <TD>" + rs.getInt("SALARY") + "</TD>");
out.println(" <TD>" + rs.getString("STARTDATE") + "</TD>");
out.println("</TR>");
}
out.println("</TABLE>");
}

catch(SQLException e)
{
out.println("SQLException: " + e.getMessage() + "<BR>");
while((e = e.getNextException()) != null)
out.println(e.getMessage() + "<BR>");
}
catch(ClassNotFoundException e)

{
out.println("ClassNotFoundException: " + e.getMessage() + "<BR>");
}
finally
{
//Clean up resources, close the connection.
if(conn != null)
{
try
{
conn.close();
}
catch (Exception ignored) {}
}
}

%>
</CENTER>
</BODY>
</HTML>

You might also like