Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dont know what is happenning

 
Thyagarajan Ramanujam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't use thread titles like that; people want to know "what is happening" before they read the thread.
 
Srikanth Basa
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using the correct DatabaseConnection classfile with your servlet ?
Please post the code.
 
Thyagarajan Ramanujam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic