I have searched far and wide everywhere, but none of the answers to this issue have helped me.

My problem: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

I have it added as an external JAR. After that didn't work, I converted my file to Maven and added the dependency for it, that still didn't help. I've tried everything people have suggested in the narrowest corners of the internet, yet to no avail.

I need some assistance.

[e]

I get that it might be required of me to rename it to com.mysql.jdbc.Driver, but it doesn't work with that name either. I renamed it cause that's the seeming path to it.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.sql.*" import="com.mysql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add a new company</title>
<link href="bootstrap.css" type="text/css" rel="stylesheet">
</head>
<body>
    <h1>New Company</h1>
    <form method="post" action="CreateNewCompany">
        <table>
            <tr>
                <td>Name</td>
                <td><input type="text" name="fullName"></td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="email"></td>
            </tr>
            <tr>
                <td>Address</td>
                <td>
                    <select class="form-control">
                        <option value="-1">Select an address</option>
                        <%

                        try{
                            String Query = "select * from addresses";
                            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
                            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/company","root","1234");
                            Statement statement = conn.createStatement();
                            ResultSet resSet = statement.executeQuery(Query);
                            while(resSet.next()){
                                %>
                            <option value="<%=resSet.getInt("addressId")%>"><%=resSet.getString("street")%></option>
                                <%
                            }
                        }
                        catch(Exception ex){
                            ex.printStackTrace();
                            out.println("Error: " + ex.getMessage());
                        }
                        %>
                    </select>
                </td>
            </tr>
            <tr>
                <td>List of Employees</td>
                <td><input type="text" name="employees"></td>
            </tr>
        </table>
        <input type="submit" value="Submit" name="submit"> <input
            type="button" value="Back" name="return">
    </form>
</body>
</html>

2 Answers

0
vicky9988 On

Here is one example to load the driver and get the connection object. check the below points

1)

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;



     public class ExampleLoadingDriver {
                public static void main(String[] args) {
                    try {
                        // The newInstance() call is a work around for some
                        // broken Java implementations

                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                    } catch (Exception ex) {
                        // handle the error
                }
            }
        }

do not import com.mysql.jdbc.*

for getting the connection object

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    Connection conn = null;
            ...
            try {
                conn =
                   DriverManager.getConnection("jdbc:mysql://localhost/exampledb?" +
                                               "user=<<userName>>&password=<<Password>>");
               ...
            } catch (SQLException ex) {
                // handle any errors            
            }

2) check jar is added in your classpath.

3) Also, try to build your project manually.

-1
Community On

It turns out that I had to download an older version of the connector and that made it work. Thank you all for your time and help!