File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC 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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Dont know what is happenning" Watch "Dont know what is happenning" New topic
Author

Dont know what is happenning

Thyagarajan Ramanujam
Greenhorn

Joined: Mar 24, 2008
Posts: 25
Hi,

I have a class(DatabaseConnection.java) 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.
Thanks


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39537
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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
Greenhorn

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
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
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: http://aspose.com/file-tools
 
subject: Dont know what is happenning
 
Similar Threads
Specifying Database url in datasource
Resource Adapter Exception in Websphere 5.0
UNmanaged JBoss Connection Pool wanted!
Problem Using DataSource and Creating Connection Pool
Connection Pooling in Websphere V3.0