wood burning stoves 2.0*
The moose likes JDBC and the fly likes Access database from a class file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Access database from a class file" Watch "Access database from a class file" New topic
Author

Access database from a class file

Emma Aziz
Greenhorn

Joined: Apr 27, 2009
Posts: 23
Hello All,

I am currently building a mini programme which connects to Oracle database. I have tried several ways and they are all successful. The ways are:

1) Connection to database and query to database in a single jsp file with action in a different html file
2) Connection to database and query to database in a single class file with action in a jsp file

Now, I would like to have action in one html file, query to database in one jsp file and a connection to database in another jsp / class file. How do I do that?

Search.html -> qrySearch.jsp -> Connection.jsp

Note that I am not familiar at all in doGet / doPost method. If someone can point out how is the step-by-step to connect to database which coded in different jsp file.

Thanks in advance.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

Hi Emma,

We do not advise that you put database code in your JSP. I am sure someone will be able to point you to a clean way to access data in your web application.

However, examples of what you describe in your question can be found by googling for "jsp jdbc tutorial".




OCUP UML fundamental and ITIL foundation
youtube channel
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Emma,

As Jan pointed out , its not advisable to use scriptlets in JSP. As a first try you could define generic DB class and get connections in Servlet and pass the values to JSP to display it.
Emma Aziz
Greenhorn

Joined: Apr 27, 2009
Posts: 23
Thanks for all the reply.

I want to access the database which all the information are stored in a class file. Maybe I stated
them worngly previously.

I have this ConnectOracle.java, compiled to a class, which the code looks like this:


and I have a jsp/html file which will do the query. This page will look for the ConnectOracle.class.

How do I call the class file from my jsp file.

I am having problem compiling the ConnectOracle package. I put "package Connect" at tge first line
but DJ Java decompiler said some problems occured while compiling. Without the "package Connect",
it can be compiled successfully.

Thanks in advance.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Emma,

I guess you are confused with .class file and .java file. To make things clearer , A java file is just a normal file which when compiled using javac (JVM provided in JDK) converts to class file. So whenever you say to call/access the java file or class file it typically means you call "class"( not class file , not java file ) , but the "class" created on compiling java file.
It always better to refer them as "classes".

How do I call the class file from my jsp file.


If you still want to use scriptlets in JSP for learning purpose , you can proceed.So how do you import classes in servlet ? similarly import them to JSP using <@%page import="" %> atttribute.




This is main method , which can be run standalone. When you run from JSP , you dont require it be standalone. JSP Container takes care of calling the required method.

Also , you can see Connection object is a local variable which created inside main , and once main method ends , Connection object is lost. ( no reference to it). So how do you use connection object to make query ?

You got to change your ConnectOracle class to be useful inside JSP/servlet or any other means.

1) Define static Connection conn object outside the method.



2) Change the main method to take no args ,( otherwise you got to pass dummy useless String array whenever you call main()) ,and change the return type to return conn object.



2) initialise the conn inside the main like you did. ( dont close the connection object) .check if the conn is already there and return , else initialise



3) Define a new method to close the connection object.




Emma Aziz
Greenhorn

Joined: Apr 27, 2009
Posts: 23
Thanks for all the replies.

I already set up my class files for Database and Query.

The database code is as below:


and the query class is as below:


The thing is, how do I call the "conn" so that it can be recognized by the 2nd servlet? Whenever I try to compile it will say cannot resolve symbol at the conn. part.

Thanks for all the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access database from a class file