Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am attemping to establish Mysql connectivity to Database using Java Struts1. Here the code snippet.Also I added mysql-connector-java-5.0.8-bin.jar to web-inf/lib folder . Also added the same to classpath variable.

import java.sql.DriverManager;

import java.sql.Connection;

import java.sql.SQLException;


public class select {

  public  void connection() {

    System.out.println("-------- MySQL JDBC Connection Testing ------------");

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;
    }.

This is Error message. What steps am I missing. Kindly suggest ASAP.

-------- MySQL JDBC Connection Testing ------------
Where is your MySQL JDBC Driver?
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at Com.Candidjava.select.connection(select.java:14)
    at Com.Candidjava.LoginAction.execute(LoginAction.java:26)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
share|improve this question
up vote 0 down vote accepted

Add mysql-connector-java-5.0.8-bin.jar to your tomcat installation directory as well. And when right click on your project and select build path then libraries. and inside libraries Apache Tomcat you should find your connector jar then only your mysql will work

share|improve this answer
    
solved TC & Rgds – ACJ Jan 31 '14 at 9:59
    
You ve to tick the correct answer that gave u the solution for this problem. – user3222718 Jan 31 '14 at 10:03

try this is an example of jdbc connection to mysql database:

import java.sql.DriverManager;

public class Connection {

private static java.sql.Connection conex = null;

public static java.sql.Connection getConnectionInstance() {
    if (conex == null) {
        try {
            // connection to database
            Class.forName("com.mysql.jdbc.Driver");
            conex = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/databaseName","root",
                    "MySQLPassword");
            return conex;
        } catch (Exception e) {
        }
    }
    return conex;
}

}

make sure you have opened the port selected during install.

share|improve this answer
    
i make this class as singleton,so I can import conex, which is used to connect to any class that needs to do the query. – OiRc Jan 31 '14 at 8:37
    
TC & Rgds solved – ACJ Jan 31 '14 at 10:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.