• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't connect to oracle DB

 
Harry Wordsworth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
So I want to make sure I am doing this the right way.
My goal:
Eventually - To create a war file and deploy it to weblogic. The War file has a simple servlet and one jsp which has a submit button. when you click the submit button in the jsp if forwards to the servlet, which connects to and pulls values from an oracle express database. First I am creating the code and testing it in eclipse.
situation so far:
In the weblogic admin console I created a datasource (testds1) which connects to the oracle express database. I tested the connection and it "appears" to work. It indicated connection successful.

The datasource has the following for URL and driver...
URL = "jdbc:oracle:thin:@//desktopcomputer1:1521/XE"
Driver = "oracle.jdbc.OracleDriver"

now, in Eclipse, I created a project containing the servlet and jsp code. I downloaded the oracle driver (ojdbc6.jar) and placed it in the build path of the project.
I also added, because of exceptions I was getting, the javax.servlet.javax.servlet-api.jar

For the connection code I have tried to use the same parameters from the datasource I created in the weblogic admin console.

What happens:
So when I run the code in eclipse the index.jsp file is brought up with a simple button, I click it and control goes to the servlet which should connect to the database. I don't have anything more complex at the moment, just to connect to the database and a println saying the connection was successful.
That code is as follows...
in the servlet, the first method that receives the call has these lines of importance...

Basically it just calls a method called getConn which gets the connection and returns it.

The getConn method is as follows...


When I run I get an exception

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
Conn = null in getConn method

and then later on in the stack trace I get ...
java.lang.NullPointerException
pointing to this line...
st = conn.createStatement();

It appears that the line System.out.println("Connected to the database") is never executed.


So this is where I need some guidance on. I thought loading the driver into the build path would resolve any issues like this.
I saw a tutorial online which showed in eclipse creating a database connection. Do I need to do that? I just thought that having oracle express running on my system and having the driver in the build path and the code correct to connect would be enough?
I know thats probably not specific enough detail for this but any help is appreciated.
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harry.

Hmmm.  Where to start.
There are a few things happening here.

First of all trying to answer your questions:
>So this is where I need some guidance on. I thought loading the driver into the build path would resolve any issues like this.
The database driver needs to be available to your server at runtime. 
Normally that means installing the database driver in the servers "lib" directory.  Did you do that for Weblogic?
If you have configured the datasource in weblogic and got a connection, that indicates that you should be ok running in that environment.

Try:  Build and deploy the your web application manually to weblogic and run it there directly (rather than from Eclipse).  Does it work as you expect?


How are you currently "running" your JSP and servlet from Eclipse?
I'm not a Weblogic user, but I have integrated Tomcat with Eclipse before using the standard WTP plugins.
When I do that Tomcat actually creates its own Tomcat configuration instance under "Servers"
That means that any configuration I have done for the "real" Tomcat installed on my system is actually ignored in favour of Eclipse's copy.
If the same thing is happening for Weblogic, then when you run your project from Eclipse it doesn't know about the datasource and/or database drivers you have configured via the admin console.


The second thing to talk about:  Getting a database connection via JNDI Datasource.
The whole point of configuring your database connection in your application server is that you then DON'T need to configure the username password etc etc in your code.
When you configure it in your app server you give it a JNDI name e.g. jdbc/NameOfMyDatasource.
Your code should retrieve the connection via that JNDI name.
So the getConnection method should look more like:




hope this helps some,
Stefan
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, when you deploy a web application to a server like WebLogic, it's not normal to include database drivers. The normal way to do things is to install the database driver into the server, as Stefan described. Then when you deploy your web application it will find the database driver because the server already has it. And notice that, again as Stefan points out, the code to access a database driver from a web application is different from the code you'd use in a standalone application.
 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why you should do this:

The brute-force DriverManager approach is all very well for stand-alone single-user programs, but webapps can have many users. It's expensive (in terms of machine resources) to create a Database connection. Not only JVM resources have to be acquired and initialied, so do OS resources such as the network sockets used to do the actual app-to-database server communications. And there are a finite number of sockets.

Instead, well-designed Enterprise webapps use Database Connection Pools. Connection Pools are constructed by the webapp server* and instead of creating/destroying Connections in the webapp, instead the webapp pulls a pre-constructed Connection from the pool, uses it and returns it (Pool or no pool, NEVER hold onto a Connection between web requests!)

Connection pools are not only more efficient, they're more flexible and more secure. Instead of having to change the URL, userid and/or password by modifying and re-compiling source code, you change the Pool using the webapp server's administrative functions. That means that application programmers don't need to know production passwords and it's possible to use the same webapp in both test and production environments without recompiling.

---
*Which is why the database driver JAR must go in the server's lib directory and not in the WAR's WEB-INF/lib directory.
 
Dave Tolls
Ranch Hand
Posts: 1914
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry Wordsworth wrote:
In the weblogic admin console I created a datasource (testds1) which connects to the oracle express database. I tested the connection and it "appears" to work. It indicated connection successful.



The issue is, as has been said, that you are not actually using this data source in your code.
You are trying to manually create a connection instead.

Using the Context code provided by Stefan will mean you are using the DataSOurce you set up in weblogic, and all will be right with the world again!
 
Harry Wordsworth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone thanks for the replies. They have helped me change my understanding of the problem I am facing.  The main points I got from the postings were...

1) Database driver JAR must go in servers lib and not the WAR's WEB-INF/lib
- check, I think I understand the reasons given on this so I will check my setup and change accordingly

2) Don't use drivermanager method
- check, I did not before but after people have explained why this is different I see now that I was
using the standalone application approach. I will look at changing per Stefan's example

3) "Try:  Build and deploy the your web application manually to weblogic and run it there directly (rather than from Eclipse).  Does it work as you expect? "
- ok I will need to try this way. This should work, Thanks! I was hoping to use Eclipse as not only a dev env but test env also like I did when
I set this up for Tomcat but...that brings up another point........

Stefan Evans wrote:
How are you currently "running" your JSP and servlet from Eclipse?
I'm not a Weblogic user, but I have integrated Tomcat with Eclipse before using the standard WTP plugins.
When I do that Tomcat actually creates its own Tomcat configuration instance under "Servers"
That means that any configuration I have done for the "real" Tomcat installed on my system is actually ignored in favour of Eclipse's copy.
If the same thing is happening for Weblogic, then when you run your project from Eclipse it doesn't know about the datasource and/or database drivers you have configured via the admin console.


Yes this is exactly what happened when I tested this for Tomcat. When I ran it in eclipse it ignored the tomcat installed on my system and used the Eclipse Tomcat instance(?). Not sure what the proper terminology is. I mean it's not running my tomcat installed on my machine but looks like it creates a separate tomcat server that is integrated into Eclipse.
For this code, that I eventually want to deploy on WebLogic, I developed it on eclipse and then when I run it, it offers me the integrated(?) tomcat server to run it on. There doesn't seem to be an integrated WebLogic server in eclipse (at least not that I know of). I was using this as a testing env because it saves a few steps as I just have to make a change in the code and run in eclipse instead of having to make changes to the code and then deploy to weblogic, restart and repeat each time I want to make a code change.
So, my mindset before this post, was that I was not planning on using the datasource I configured in weblogic "yet". That would be something once I tested making the connection to my running oracle instance with the code in eclipse then once I knew at least that could work I would make whatever changes were
needed to use the datasource in weblogic and deploy the code to weblogic.

 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Eclipse WTP plugin (pre-installed in the Eclipse J2EE spin) does use your installed Tomcat, but when it runs Tomcat, it does several horrible things to the Tomcat configuration. So if you wondered why your Tomcat server's configuration doesn't work in Eclipse, blame WTP. The sysdeo/Mongrel Eclipse plugin is much better behaved when used to run Tomcat from inside Eclipse, but it's not pre-installed. You have to download and install it manually.

I'm hoping that the WTP interaction with WebLogic is more civilized, but I've never tried it. For WebSphere and WebLogic I tend to use the Eclipse external app debugger instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic