JSP +JDBC connectivity with Oracle 91
I am trying to connect JSP page to the oracle database table using jdbc thin driver...which i am not able to do...i think its not loading the driver.. Here is the code:
I am not getting any exceptions also
<%@page contentType="text/html"%>
<%@ pageimport ="java.sql.*"%>
<%
String empno =null;
String empname =null;
%>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:ORACLE","scott","tiger");
// @machineName:port:SID,userid, password
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select empno from empDetails");
while (rs.next())
empno = rs.getString(0);
empname =rs.getString(1);
stmt.close();
}
catch(Exception e){
e.printStackTrace();
}
%>
<%=empno%>
<%=empname%>
i am getting the output as null null that is due to the variables i took for storing the value from the database..
I am using oracle 9i.
MY database name is oracle.
pls let me know whether my driver class and the urlis correct if not what shud i use and do i need to give anything in the classpath settings.
Pls help me with this ......what is the problem with my code.
vinay
First, you should look at your Tomcat log files ... you are most likely getting an Exception, which will be the reason why the empno and empname are remaining null.
[TOMCAT_HOME]/logs/stdout.log
Second, your code didn't have braces ({}) around the while statements, so only the first would ever get executed ... and your select statement (query) was only ever returning ONE field (empno)...
Try the following after you have checked your log files.
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from empDetails");
while ( rs.next() )
{
<%= rs.getString("empno") %>
<%= rs.getString("empname") %>
<%= "<br>" %>
}
stmt.close();
hey thanq for replying ....that problem is solved...actually it was not taking the driver before...now a new problem pls help me if u r freee...
<html>
<head><title>oracle</title></head>
<body>
<%@ page language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="oracle.jdbc.driver.OracleDriver" %>
<%
try
{
DriverManager.registerDriver(new OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle","scott","tiger");
PreparedStatement ps = con.prepareStatement("select * from empDetails");
ResultSet rs = ps.executeQuery();
ResultSetMetaData rm = rs.getMetaData();
int cols = rm.getColumnCount();
out.println(cols);
[b]while(rs.next())[/b]{
for(int i=1; i<=cols;i++){
out.println("hai");
out.println(rs.getString(1));
out.println(rs.getString(2));
}
}
rs.close();
ps.close();
con.close();
}
catch (Exception e){
out.println(e);
}
%>
</body></html>
this is working fine without exceptions and i am sure the database connection is also established...but the problem now is the control is not going into the while(rs.next()) that out.println("hai"); is working fine..there must be some logical problem i am trying since long time but not successful...pls help me
vinay
Well iam trying to run this code but everytime i find exception "Driver not found".So plz. help me out. Though this code is running fine if i am using it in simple java class.
<%@page language="java" %>
<%@page import="java.sql.*" %>
<%
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e){
e.printStackTrace();
out.println("Driver not found");
}
try{
Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@training1:1521:trng","scott"
,"tiger");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select *from User_Auth");
while(rs.next()){
out.println(rs.getInt(1));
out.println(rs.getString(2));
}
}catch(SQLException e){e.printStackTrace();
out.println("Cannot Access Database");}
out.println("It is Finished.");
%>