1

I am designing a registration page using MVC design pattern. I have made a class file which will input the parameters into the database using sql commands but i am getting

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Here is the code

package src.service;

import java.sql.*;

public class RegisterService {

    public void addToDatabase(String name, String id, String email, String     password){
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            // Get a connection to the database
            Connection myConn =     DriverManager.getConnection("jdbc:mysql://localhost:3306/chillmaarodb", "root", "rsystems");

            // Create a statement

            Statement myStatement = myConn.createStatement();

            String sql = "insert into userid values(" + id + ", '" + name + "', '" + email + "', '" + password + "')";

            myStatement.executeUpdate(sql); 


        }

        catch (Exception e){

            e.printStackTrace();

        }

    }

}

I have imported the driver in my lib folder of the project, imported it in build path, imported it in tomcat server in the folder tomcatv7>lib by creating a lib folder. Still it is showing the same error. Kindly help.

psr
  • 2,619
  • 4
  • 32
  • 57
  • possible duplicate of [java.lang.ClassNotFoundException: com.mysql.jdbc.Driver (in jre's libs)](http://stackoverflow.com/questions/10602899/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-jres-libs) – Jens Jul 29 '15 at 09:21
  • JDBC driver JARs belong in the Tomcat server /lib folder. It sounds like you created a subfolder /lib/lib. Don't do that. – duffymo Jul 29 '15 at 09:22
  • mysql-connector-java-5.1.18-bin.jar is present in classpath? – VedantK Jul 29 '15 at 09:38

3 Answers3

1

You should add MYSQL JDBC LIBRARY to your project and also import

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
Ajmal Muhammad
  • 685
  • 7
  • 25
1

You need to setup the DB Connection in server.xml follow this tutorial : http://examples.javacodegeeks.com/core-java/mysql-connector-for-java-how-to-install-in-eclipse-and-tomcat/ and https://www.mulesoft.com/tcat/tomcat-mysql

as well as you need to download MySQL Connector from: http://dev.mysql.com/downloads/connector/j/ and copy the jar file to "C:\tomcat7\lib"

Abdullah AlHazmy
  • 1,299
  • 1
  • 13
  • 22
0

This worked for me---

This solution is only for Dynamic web projects.

Steps--

1)Create a Dynamic Web project

2)I added the "mysql-connector-java-5.1.48-bin" jar in WebContent/WEB-INF/lib folder.

2) Create a tomcat server

3)inside src create a demo servlet--

package com.luv2code.testdb;

import java.io.IOException;
import java.io.PrintWriter;

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

import java.sql.*;

/**
 * Servlet implementation class TestDbServlet
 */
@WebServlet("/TestDbServlet")
public class TestDbServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // setup connection variables
        String user = "springstudent";
        String pass = "springstudent";

        String jdbcUrl = "jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimezone=UTC";
        String driver = "com.mysql.jdbc.Driver";

        // get connection to database
        try {
            PrintWriter out = response.getWriter();

            out.println("Connecting to database: " + jdbcUrl);

            Class.forName(driver);

            Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);

            out.println("SUCCESS!!!");

            myConn.close();

        }
        catch (Exception exc) {
            exc.printStackTrace();
            throw new ServletException(exc);
        }


    }

}

4)Just right click and run as run on server select ur tomact server

Remember before all these you need to create ur db schema, here i have used mysql workbench. Mysql part is not covered in this answer.

If this does not work, try adding the msql connector jar inside tomcat/lib folder

Arpan Banerjee
  • 826
  • 12
  • 25