Results 1 to 9 of 9
  1. #1
    wiz6 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default jdbc works in app but not in servlet

    My postgresql jdbc driver fails if i use servlet but works fine when i use app.
    Exception gives error: org.postgresql.Driver

    What should i do?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: jdbc works in app but not in servlet

    There must be more than just org.postgresql.Driver
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    wiz6 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: jdbc works in app but not in servlet

    My code getConnection()
    Java Code:
    public static Connection getConnection() {
    		Connection db_connection=null  ;	
    		String pwd = "";
    		String usr = "";
    		String url = "";
    		
    		try
    		{            
    			ResourceBundle bundle = ResourceBundle.getBundle("DBConnection");
    			Class.forName(bundle.getString("Driver"));
    			url = bundle.getString("url");
    			usr = bundle.getString("usr");
    			pwd = bundle.getString("pwd");
    			db_connection = DriverManager.getConnection(url, usr, pwd);
    		}
    		catch(Exception ex)
    		{  
    			 MyLogger.log("dbconnection.getConnection():" + ex.getMessage());
    			 }
    		return db_connection;
    	}
    MyLogger shows in log.txt file error: dbconnection.getConnection():org.postgresql.Driver
    It only appears when i try to run servlet. If i run application, database works fine.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,061
    Rep Power
    4

    Default Re: jdbc works in app but not in servlet

    That is because you log only the exception message. You need to log the entire exception so you get it including stacktrace. I have no idea what kind of logging stuff you're using, but I advise you to use a standard logging mechanism such as log4j in stead of rolling your own.

    I'm giving it a 99% chance that it is in fact a ClassNotFound, meaning your JDBC driver is not where it is supposed to be or missing entirely.

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: jdbc works in app but not in servlet

    Log more, you need the full exception and stack trace.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    wiz6 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: jdbc works in app but not in servlet

    I fount the solution. I havent copied my postgresql driver to tomcat/bin folder. I copied it to project build path, but not to tomcat/bin...
    Whats the differences between coping to project build path and tomcat/bin folder?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,061
    Rep Power
    4

    Default Re: jdbc works in app but not in servlet

    no, that was not the solution. Tomcat does not look in the bin directory for jars, it should have been in tomcat/lib.

    And btw: if you would have mentioned that you were using Tomcat right from the beginning, you would have had a more direct answer sooner too. It is really not beneficial to you to leave away such details.

  8. #8
    wiz6 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: jdbc works in app but not in servlet

    yeah, sorry, tomcat/lib, not tomcat/bin :)
    okay, next time i will be smarter :)

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,061
    Rep Power
    4

    Default Re: jdbc works in app but not in servlet

    Wiser, not smarter. People who are incredibly intelligent can and will still do very foolish things ;)

    I neglected to answer this earlier question:

    >Whats the differences between coping to project build path and tomcat/lib folder?

    Likely you created a datasource for your database; this datasource is not part of your application but is managed by the server. So it is also the server that needs the JDBC driver, not only your application.
    Last edited by gimbal2; 07-11-2013 at 10:04 AM.

Similar Threads

  1. Servlet and jdbc connectivity
    By user07 in forum Java Servlet
    Replies: 1
    Last Post: 11-12-2012, 11:17 AM
  2. JDBC Error in servlet
    By shuklagirish in forum Java Servlet
    Replies: 10
    Last Post: 05-17-2011, 06:31 PM
  3. Jdbc and servlet
    By ulferik in forum New To Java
    Replies: 1
    Last Post: 03-26-2011, 09:01 AM
  4. Servlet JDBC
    By kevinnrobert in forum Java Servlet
    Replies: 1
    Last Post: 04-02-2010, 01:27 PM
  5. Problem in Servlet with JDBC
    By *New Programer* in forum Java Servlet
    Replies: 2
    Last Post: 12-20-2007, 09:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •