Results 1 to 6 of 6
  1. #1
    JavaJoe is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Having trouble getting database connection in Dynamic Web Project

    Hi,
    I've been around java for some time, but new to web development.
    I'm trying to create my first "Dynamic Web Project" using Eclipse Juno, tomcat, and mysql 5.
    Could anyone tell me what is required to setup a connection between my project and mySQL
    that would include the following:
    1.) What is required in the Data Source Explorer?
    2.) What is required in setting up my connector J Driver for this project?
    3.) What *.xml files need to be edited and how?
    4.) What am I doing wrong in the bellow code I'm using to test my connection?

    My JSP Index page code:
    Java Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1" import="java.sql.*,certstudyconnection.*"%>
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     <html>
     <head>
     <%
      Connection conn = null;
      MyCertStudyDBConnection cscon = new MyCertStudyDBConnection();
      conn = cscon.getLocalConnection();
      Statement stmt = conn.createStatement();
      String sqlString = "SELECT userName FROM Users";
      ResultSet rset = stmt.executeQuery(sqlString);
      while (rset.next())
       System.out.println(rset.getString("userName"));
     %>
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <title>JDBC-CONNECTION-TEST</title>
     </head>
     <body>CertStudy Database Connection
     </body>
     </html>
    My java class for database connection code:
    Java Code:
    package certstudyconnection;
     import java.sql.Connection;
     import java.sql.DriverManager;
     import java.sql.SQLException;
     public class MyCertStudyDBConnection {
      Connection con = null;
      
      public Connection getLocalConnection() {
       
       try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/certstudy","<user name>","<password>");
       } catch (ClassNotFoundException enfc) {
        System.out.println("ClassNotFoundException in getConnection, " + enfc.getMessage());
       } catch (SQLException sqle) {
        System.out.println("SQLException in getConnection, " + sqle.getMessage());
       }
       
       return con;
      }
     }
    Here is my current error displaying in my Eclipse console window:
    INFO: Server startup in 534 ms
    ClassNotFoundException in getConnection, com.mysql.jdbc.Driver
    Feb 25, 2013 9:51:06 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [jsp] in context with path [/certificationace] threw exception [java.lang.NullPointerException] with root cause
    java.lang.NullPointerException
    at org.apache.jsp.Index_jsp._jspService(Index_jsp.jav a:72)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run Task(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    Any help understanding a solution will be greatly appreciated.
    Many thanks in advance!
    -JavaJoe
    Last edited by JavaJoe; 02-25-2013 at 05:37 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Having trouble getting database connection in Dynamic Web Project

    Please wrap your code in [code] tags [/code] so it retains its formatting.

    For your stack trace, you need to have the driver jar file on the classpath.
    For a web application that means that it needs to be in the WEB-INF/lib directory of your web app.
    Tomcat picks up all the jar files in there when it starts up.

    For the data source, click the New Connection Profile button and fill in the bits relevant to your database.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JavaJoe is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Having trouble getting database connection in Dynamic Web Project

    Thanks for the reply Tolls.
    I currently have my mysql driver located in the following folder path in eclipse:
    project/JavaResources/Libraries/mysql-connector-java-5.1.22-bin.jar
    How would I get this to be in the project/WebContent/WEB-INF/lib?

    -JavaJoe

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Having trouble getting database connection in Dynamic Web Project

    No idea I'm afraid.
    If you set up a data source instead and utilise that instead of creating a Connection manually then I suspect Eclipse will deploy it all for you.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JavaJoe is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Having trouble getting database connection in Dynamic Web Project

    After you mentioned this I found a way to get it there manually by coping and pasting my driver jar file
    to my project/WebContent/WEB-INF/lib folder via my workspace path.
    But when i reopened Eclipse it still doesn't show up in the tree structure of the project explorer.

    since I've been trying just about everything, should I delete the database setup in my Eclipse 'Data Source Explorer' and
    also delete the driver instance showing up in the project explorer path of project/JavaResources/Libraries/mysql-connector-java-5.1.22-bin.jar?

    Thanks again for your assistance.
    -JavaJoe

  6. #6
    JavaJoe is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Having trouble getting database connection in Dynamic Web Project

    Hi Tolls,

    I just stumbled on to figuring out the last bit to get it working... :-)
    I removed the previouse project/JavaResources/Libraries/mysql-connector-java-5.1.22-bin.jar file.
    keeping in the one I manually droped into my workspace path project/WebContent/WEB-INF/lib folder.
    Then I accidentally right clicked on the project/WebContent/WEB-INF/lib folder in my Eclipse 'Project Explorer' tree
    and found a new item I could choose called 'refresh'.
    I clicked it and the driver jar file showed up magically.
    I re-tested and all is grand and the sun is shining once again. :-)
    Thanks for your help, together we did it!
    Regards,
    JavaJoe

Similar Threads

  1. Dynamic web project using separate Javascript
    By alexisjk in forum Advanced Java
    Replies: 1
    Last Post: 02-01-2012, 08:46 PM
  2. run dynamic web project from doube-clicking an icon
    By SoftDeveloper in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-09-2011, 10:06 PM
  3. pb of deployment of a dynamic project
    By skizz in forum Eclipse
    Replies: 0
    Last Post: 08-22-2011, 04:02 PM
  4. Eclipse dynamic web project problem
    By Yuvarajsinh in forum Eclipse
    Replies: 0
    Last Post: 06-12-2011, 06:49 AM
  5. Replies: 4
    Last Post: 04-22-2011, 07:24 PM

Posting Permissions

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