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.

3
  • possible duplicate of java.lang.ClassNotFoundException: com.mysql.jdbc.Driver (in jre's libs) Commented Jul 29, 2015 at 9:21
  • JDBC driver JARs belong in the Tomcat server /lib folder. It sounds like you created a subfolder /lib/lib. Don't do that. Commented Jul 29, 2015 at 9:22
  • mysql-connector-java-5.1.18-bin.jar is present in classpath? Commented Jul 29, 2015 at 9:38

3 Answers 3

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"

Sign up to request clarification or add additional context in comments.

Comments

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;

3 Comments

i have imported java.sql.* without any compile time error but in my libraries folder there is no such mysqllibrary.
if no libraray found. you should download the jar file (JDBC driver JAR). are you using NETBEANS IDE ?
actually there was a problem in connection of tomcat and jdbc.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.