aspose file tools*
The moose likes JDBC and the fly likes Java to Display Data from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Java to Display Data from Database" Watch "Java to Display Data from Database" New topic
Author

Java to Display Data from Database

Emma Aziz
Greenhorn

Joined: Apr 27, 2009
Posts: 23
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39861
    
  28
This question would sit better on our databases forum. Moving.

Have you seen the databases part of the Java™ Tutorials, or the JDBC documentation?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

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

Joined: Apr 27, 2009
Posts: 23
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

Joined: Jan 01, 2009
Posts: 874



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

Joined: Apr 27, 2009
Posts: 23
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

Joined: Jan 01, 2009
Posts: 874


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");
%>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java to Display Data from Database