• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Java to Display Data from Database

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I would like to ask one question on Java. I am extremely new in Java and I need help. I want to display data from my database which is Oracle. My code looks like this:



At first, I set the System SDN as Ora10g and all my data from the database can be obtained through the command prompt. But, I don't know what happen now, suddenly the System SDN, even I have entered the data source name a few times, it still shows blank (empty). When I googled, I need some rights to enter System SDN. Unfortunately, I'm using AD ID and will not get the rights to what I want.

So, I tried to set the classpath for the driver manually by typing into the command prompt
"javac -classpath=C:\oracle\product\10.2.0\client_5\jdbc\lib\ojdbc14.jar tryout.java"
while compiling and it compiled successfully.

But, whenever I try to run by typing at the command prompt "java -cp . tryout" I will get "Could not find the database driver" which is from my Exception.

Why is this happening? Why is my program straight away go to exception wherelse I have set the classpath.

I hope people here will help me because I have searched halfway around the world and still could'nt find the solution.

Thanks in advance.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post source code; I added them in your post above so that the code looks nice.

When you run the application, you must also make sure that the JAR file with the JDBC driver is in your classpath. Try running it like this:

java -cp C:\oracle\product\10.2.0\client_5\jdbc\lib\ojdbc14.jar;. tryout

A note about your import statements:

The first line already includes eveything from the package java.sql. It makes lines 2, 3 and 4 superfluous.
 
Marshal
Posts: 80754
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question would sit better on our databases forum. Moving.

Have you seen the databases part of the Java™ Tutorials, or the JDBC documentation?
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emma Aziz wrote:


So, I tried to set the classpath for the driver manually by typing into the command prompt
"javac -classpath=C:\oracle\product\10.2.0\client_5\jdbc\lib\ojdbc14.jar tryout.java"
while compiling and it compiled successfully.

But, whenever I try to run by typing at the command prompt "java -cp . tryout" I will get "Could not find the database driver" which is from my Exception.

Why is this happening? Why is my program straight away go to exception wherelse I have set the classpath.
.



Emma,

"java -cp . tryout" , will actually look for Oracle driver inside the current package. So add classpath to either default CLASSPATH variable or when you run java , use -cp C:\oracle\product\10.2.0\client_5\jdbc\lib\ojdbc14.jar and "." current directory.
try like

java -cp C:\oracle\product\10.2.0\client_5\jdbc\lib\ojdbc14.jar;. tryout


EDIT : Oh yeah , Jesper had already given clue . dint check it !!
 
Emma Aziz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the suggestions!

I have tried the java -cp C:\oracle\product\10.2.0\client_5\jdbc\lib\ojdbc14.jar;. tryout and it successfully displayed all the data from the database.

I have one more problem regarding this particular code...

I put this code into a jsp file, which looks like this (I have learnt how to put the code into the code tags ):



But the only thing came out at the Web is "Welcome to Emma's page" (2x because I put it at the first and last line).
Anything between try and catch didn't come out as expected, even the "out.println("Test1")". Is this anything to do with my driver too?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Anything between try and catch didn't come out as expected, even the "out.println("Test1")". Is this anything to do with my driver too?



Not with the Database driver. My first thought is the scriptlets disabled ? you can check in web.xml . The entry yo check is
scripting-invalid>true</scripting-invalid>. But i wonder that it is not disabled by default.
 
Emma Aziz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balu,

Thanks for your reply. But, which web.xml should I refer to? Is it under the WEB-INF of our application directory or in the CATALINA-HOME\conf?

Under the CATALINA-HOME\conf the code of the web.xml looks like this (the scripting invalid is nowhere to be found in the web.xml):



Whereas my web.xml under the WEB-INF looks like this:



That's it. I tried few methods but still cannot get in the way for 2 weeks already

Thanks.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emma,

First thing it is not adviced to use scriplets anymore in JSP. and Use EL , JSTL instead , there are many links in JSP FAQ link at the top. Use Servlet or DAO to obtain connection and store the values needed by view component (JSP).

But for testing purpose you could do like this and see if you get simple JSP to see if its getting rendered to HTML in the browser.

<%
Welcome to Emma Page !!
out.println("Test1");
%>
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic