File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Dont know what is happenning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Dont know what is happenning" Watch "Dont know what is happenning" New topic

Dont know what is happenning

Thyagarajan Ramanujam

Joined: Mar 24, 2008
Posts: 25

I have a class( which has methods which do database stuff(loading database driver, creating connection to the database and executing some queries).
I am using MS access database and the datasource is created properly. "sun.jdbc.odbc.JdbcOdbcDriver" is the driver.

The problem is when i use the class from any servlet to get the connection to the datasource, the connection object is returned as null. But when i put a static main method in the same class and try to access the database, it works fine.

I could not find the reason and i could not get any help.

Any help is appreciated.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
By "datasource", do you mean a DSN? If so, make sure it is set up as a system DSN, not a user DSN.

Note that the JDBC/ODBC bridge is not thread-safe, so you will have problems in an inherently multi-threaded environment like a servlet container.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
Please don't use thread titles like that; people want to know "what is happening" before they read the thread.
Srikanth Basa
Ranch Hand

Joined: Jun 06, 2005
Posts: 241
Are you using the correct DatabaseConnection classfile with your servlet ?
Please post the code.
Thyagarajan Ramanujam

Joined: Mar 24, 2008
Posts: 25
I apologize for not using meaningful subject lines.

import java.sql.*;

public class DatabaseConnection {

public Connection getConnection() throws Exception
Connection con = DriverManager.getConnection("jdbcdbc:database");
return con;

/*public static void main(String... args) throws Exception
Connection con = new DatabaseConnection().getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from UserDetails");

//Iterating the result set and displaying the details.


Yes. I mean DSN. I have set the datasource as User DSN. But if it is the case, I should not get the connection object when i run as the class as Stand Alone(calling the static main method).

Why this is happenning when the method is called from Servlet. Any way, I will try to make the datasource as System DSN and try it again.
I agree. Here's the link:
subject: Dont know what is happenning
It's not a secret anymore!