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

[1860 byte] By [vinaygadirajua] at [2007-10-2 8:19:28]
# 1

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();

munyula at 2007-7-16 22:18:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

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

vinaygadirajua at 2007-7-16 22:18:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
oracle is a big hairy dog i wouldn't bother with it unless you really have a need. try mysql. But i'm glad you got it sorted.
spear_arrowa at 2007-7-16 22:18:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
also some suggestions remove the for loop it just adds complexity just stick a println statement inside the while rs.next loop
spear_arrowa at 2007-7-16 22:18:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Though it is not a good practice to mix the business logic and the view logic in the same file. You should separate it by writing a separate java class for fetching the data from database and then call your class from the JSP page.

Now , concerning your problem you can see whether required jar files in the classpath or not and see the log files of your server.

Prashant001a at 2007-7-16 22:18:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

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.");

%>

adilk@mastek.coma at 2007-7-16 22:18:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...