Derby Driver class not found exception in java

41 Views Asked by At

I am creating my Java project with Servlet and Derby. I am trying to connect the database to the java code. This is the code I am using :

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Connection con;
        try{
            // Connect to the database to check if the user exists
            System.out.println(System.getProperty("java.class.path"));
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            con = DriverManager.getConnection("jdbc:derby://localhost:1527/rubbish");

            // Query the database to get the user
            String query = "SELECT * FROM users WHERE username = '" + username + "'";
            Statement stmt = con.createStatement();
            ResultSet res = stmt.executeQuery(query);
            // Other code
        }catch (SQLException | ClassNotFoundException e){
            e.printStackTrace();
        }

And I get the following error when the line Class.forName("org.apache.derby.jdbc.ClientDriver"); is hitted:

java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1420)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1228)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:391)
    at java.base/java.lang.Class.forName(Class.java:382)
    at com.projects.intro_prog_web_29.Login.doPost(Login.java:24)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:514)

The previous line prints this:

/opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar

In my Intellij Mavean interface, the dependecy of derby appears, so I think that it is installed correctly.

Then I also added this to pom.xml:

<dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.16.1.1</version>
        </dependency>

So, I don't really know what is missing

I am expecting that the database connection is established, and then I can query it, but it is not recognizing the driver, so I don't know what to do.

If someone can help me, please, I am in a hurry

Thank you a lot

0

There are 0 best solutions below