aspose file tools*
The moose likes Servlets and the fly likes Database connection gives exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Database connection gives exception" Watch "Database connection gives exception" New topic
Author

Database connection gives exception

Rukmini Nagalla
Greenhorn

Joined: Apr 29, 2005
Posts: 11
I am using tomcat 5.5. I am trying to connect to sql server 2000 database and I use microsoft sql server 2000 driver for jdbc. I have all the jar files in the driver's lib folder copied to CATALINA_HOME\common\lib folder. I added the following in \config\server.xml

<Context path="/mini" reloadable="true">
<Resource name="jdbc/database_access" auth="Container"
type="javax.sql.DataSource" username="anil" password="nacware"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://localhost:1433;database=mini"
maxActive="8" maxIdle="4"/>
</Context>
I created a mywebapp.xml file in config\CATALINA\localhost and it's contents are:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/ora">
<ResourceLink name="jdbc/database_access" global="jdbc/database_access" type="javax.sql.DataSource" />
</Context>

I have added the following in /WEB-INF/webapp.xml :

<resource-ref>
<res-ref-name>jdbc/database_access</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

when I run the login servlet which checks the database for user name and passwords, I get this exception:
root cause:
java.lang.NullPointerException
LoginAction.execute(LoginAction.java:37)

Any help is appereciated.
Thanks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Is LoginAction one of your classes? What is on and around line 37?
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

I would suggest you do the following.

1. While using tomcat, please ** do not ** edit server.xml to define your context. Define your context in <your_context>.xml and place it in conf/catalina/<host_name> directory (<host_name> would be localhost in your case).

2. You **need not** define any <resource> elements in your web.xml. All resources shall be defined in <your_context>.xml. The following is a sample context.xml that adds a Oracle jdbc connection pool to your web application.



In the above, docBase refers to the directory where you have stored your web application. Not necessary to define <resource> elements in webapps

3. In #2 above, the resource set up is internal to your web application. If you wish that a particular resource be available to all web applications running on the container, heres how you go about

i. in server.xml, define the <resource> element under the <GlobalNamingResources> element.



ii. In your context.xml, define a resource-link element.



That's it - again no need to add resource elements in your web.xml

Note:: The above snippet configures an Oracle datasource. Tweak it appropriately to suit you needs.

Cheers,
Ram.
Rukmini Nagalla
Greenhorn

Joined: Apr 29, 2005
Posts: 11
Thanks Ram, it worked.
 
Don't get me started about those stupid light bulbs.
 
subject: Database connection gives exception