Results 1 to 3 of 3
  1. #1
    Praveen Verma is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Default mysql eclipse classpath ubuntu problem

    Hi everyone, I am beginner in advanced java. I am using Ubuntu 12.04 LTS, mysql 5.5.32. I am trying to create a connection between mysql and servlet in JAVA EE eclipse .

    I have done some steps given below:

    a) added the mysql-connector-java-5.1.16.jar in the eclipse Project->Properties->Java Build path->Libraries->Add External JARs

    b) I have set the classpath in .bashrc file using command
    CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.16.jar
    export CLASSPATH
    and then executed it using command
    source ~/.bashrc

    and then I have checked it using echo $CLASSPATH it displays
    :/usr/share/java/mysql-connector-java-5.1.16.jar

    Here are my servlet file :

    MySqlConnection.java

    package com.student.servlet;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * Servlet implementation class MySqlConnection
    */
    public class MySqlConnection extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public MySqlConnection() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    try{
    out.println("<HTML><BODY>");
    out.println("Trying to load mysql driver");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("Select * from emp");
    while(rs.next()){
    out.println("ID " + rs.getInt(1));
    out.println("Name " + rs.getString(2));
    }

    }catch (Exception e) {
    e.printStackTrace();
    // TODO: handle exception
    }
    out.println("</Body></html>");
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

    }


    Now I am getting the error in console of ECLIPSE

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1526)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:188)
    at com.student.servlet.MySqlConnection.doGet(MySqlCon nection.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:679)


    I think, classpath is not set. So please suggest and help.

    Thanks in ADVANCE

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: mysql eclipse classpath ubuntu problem

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: mysql eclipse classpath ubuntu problem

    Quote Originally Posted by Praveen Verma View Post
    I think, classpath is not set. So please suggest and help.
    Tomcat manages the classpath. What you need to do is put the driver jar on the classpath according to the web application packaging rules described in numerous books, articles and in the Tomcat documentation. In this case, since you seem to be creating connections in your application if I believe the stacktrace.

    Application Developer's Guide (7.0.42) - Deployment

    How to properly package and deploy web applications from Eclipse is a whole different matter requiring completely different documentation. Plenty of resources on the net though, with pictures.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. java mysql ubuntu problem
    By maxxx12345 in forum JDBC
    Replies: 5
    Last Post: 12-06-2012, 04:23 PM
  2. Using mysql in Java on ubuntu
    By dearvivekkumar in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 11-30-2011, 10:36 AM
  3. eclipse shiro classpath problem...
    By kievari in forum Eclipse
    Replies: 4
    Last Post: 09-28-2011, 01:25 PM
  4. setting classpath for java in ubuntu
    By kulangotski in forum Java Servlet
    Replies: 0
    Last Post: 01-03-2011, 03:18 AM
  5. mysql classpath ubuntu problem
    By munish in forum JDBC
    Replies: 18
    Last Post: 08-22-2010, 02:55 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
  •