facebook

How to load a JDBC driver within a class in a web project?

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #227162 Reply

    jwach366
    Member

    I am running Eclipse 3.0.1 with MyEclipse 3.8.4 on a Pentium 4 machine running Windows XP Pro. I have MySQL 4.1 and JBoss 4. I’ve created a Web Project and I want to connect to MySQL from within a method in a class that is called by a servlet. In that method, I have:
    try {
    Class.forName(“com.mysql.jdbc.Driver”);
    }
    catch (ClassNotFoundException e) {
    System.out.println(“unable to load Driver Class ” + e.getMessage());
    return;
    }
    e.message() says ‘No ClassLoaders found for: com.mysql.jdbc.Driver’. I’ve verified that I can get to the database just fine after loading the driver using the above Class.forName (..) from a vanilla java program in a Java Project in MyEclipse. I’ve tried putting the location of the Driver on the classpath of my Web Project (I highlight my Web Project in Package Explorer, click on Project>Properties>Java Build Path>Add External JARs.. then add the path to the MySQL Driver. This didn’t help. I know that Class.forName is not really the way to do this from within an EJB Project but I don’t know if my Web Project needs to be part of an EJB Project in order to successfully get to MySQL. Also, I don’t really know how to load the JDBC driver for MySQL connector by a
    means other than Class.forName(..). I don’t find anything in the Database Explorer Guide nor the Application Development Guide that addresses this. Can you straighten me out?

    THANK you very much in advance-

    #227171 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft Dev, nothing to do with ME.

    jwach, here are some quick suggestions:
    1) The driver needs to be in your <tomcat>\common\lib directory, what you have done (adding it to build path) ONLY Makes it valid during dev time, as soon as you launch the app server, it has no knowledge of that Build Path you setup within Eclipse, so when it see’s Class.forName it is like “What the hell?”

    2) I would suggest you take a look at the connection pool your app server provides. In this case in Tomcat it is a real-only JNDI context you can setup called a “data source”. Tomcat will automagically pool connections to your DB for you. Don’t get sucked into managing connections in your actual EJB/Struts/whatever code. Let your app server do it for you. (Take a look at the Tomcat data source docs).

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: How to load a JDBC driver within a class in a web project?

You must be logged in to post in the forum log in