aspose file tools*
The moose likes JSP and the fly likes connecting to database! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "connecting to database!" Watch "connecting to database!" New topic
Author

connecting to database!

honey singh
Ranch Hand

Joined: Apr 26, 2002
Posts: 44
I'm trying to connect to database using JSP using the following code
<%
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String cmd="";
try{
Class.forName("sun.jdbc.odbc.Jdbc.OdbcDriver");
con=DriverManager.getConnection("jdbc dbc:stud","scott","tiger");
stmt=con.createStatement();
cmd="select * from xyz";
rs=stmt.executeQuery(cmd);
while(rs.next())
{
String user_id=rs.getString("reg_id");
String user=rs.getString("name");
String concat=user+" _ "+user_id;
%>
out.println(concat);
<%
}
}catch(Exception e){out.println(e);}
rs.close();
stmt.close();
con.close();
%>
If I try to retrieve the result using simple class and compile using jdk,then I get the result..but if I do it with the help of JSP I get the following error:
java.lang.NullPointerException
at org.apache.jsp.file_0005fup$jsp._jspService(file_0005fup$jsp.java:116)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
at java.lang.Thread.run(Thread.java:479)

I had also put the classes111.zip file in the classpath.
please if somebody can help me.
thanx in advance for the favourable response!
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

What is your database version?
I can see that you are using the JDBC drivers from Oracle for JDK 1.x - is that correct.
I think you should use classes12.zip witch is for JDK 1.2.x and up.
You can maybe use this link as well:
http://www.coderanch.com/t/79839/Oracle-OAS/JDBC-Oracle-Host
/Rene


Regards, Rene Larsen
Dropbox Invite
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You can also try this link:
http://www.coderanch.com/t/297455/JDBC/java/No-suitable-driver
/Rene
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi,
Do yo uhave DSN created with name as Stud ?
If not then create the DSN for the oracle database.
Cheers.


SCJP, SCWCD
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
Try taking the classes12.zip out of the classpath and putting the jar in the server's WEB-INF/lib directory. You should not put the jar explicitly in the server's classpath if you can help it. Also, in weblogic, for instance, some application servers have their own DB driver packages installed. So you have to make sure that it is finding you classes first. What application server are you using? Can you declare and use a data source (using jndi) managed by the app server?
Sean
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
Originally posted by harmanjeet s:
I'm trying to connect to database using JSP using the following code
<%
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String cmd="";
try{
Class.forName("sun.jdbc.odbc.Jdbc.OdbcDriver");
con=DriverManager.getConnection("jdbc dbc:stud","scott","tiger");
stmt=con.createStatement();
cmd="select * from xyz";
rs=stmt.executeQuery(cmd);
while(rs.next())
{
String user_id=rs.getString("reg_id");
String user=rs.getString("name");
String concat=user+" _ "+user_id;
%>
out.println(concat);
<%
}
}catch(Exception e){out.println(e);}
rs.close();
stmt.close();
con.close();
%>
If I try to retrieve the result using simple class and compile using jdk,then I get the result..but if I do it with the help of JSP I get the following error:
java.lang.NullPointerException
at org.apache.jsp.file_0005fup$jsp._jspService(file_0005fup$jsp.java:116)

I dont think the problem is with the driver. The most obvious problem is the line:
String concat=user+" _ "+user_id;
%>
out.println(concat);
<%
which of course should print out the user name and id but will actually print the line
"out.println(concat);" since you are going out of the jsp brackets just before it so it will be treated as HTML.
I think the NullPointerException is probably because you've got the column names in the database wrong though. Unless you can put loggin or system.out calls to check which object is null or alternatively check the created source code in the work/ directory, its difficult to say more but check the column names and put in system.out calls first.


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
arun prasad
Greenhorn

Joined: Jun 16, 2002
Posts: 7
hello harman,
i think the problem is with the code
the code given by u is :-
---------------------------
Class.forName("sun.jdbc.odbc.Jdbc.OdbcDriver");
In this it should be
sun.jdbc.odbc.JdbcOdbcDriver
not like ur's
check that may be it may solve problem
arun prasad
 
Don't get me started about those stupid light bulbs.
 
subject: connecting to database!