vendredi 26 mars 2021

Java MySQL Connection failed

I am trying to build a web application, which should be very simple. But I'm stuck at building a login site.

I've built a mysql database quizdb with the table users_db. When I run this code in a normal java project, everything works perfectly fine. But when I run it in a JavaEE Project with Tomcat 9 and MySQL 8.0.23- I get the exception: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. The libraryy "mysql:mysql-connector-java:8.0.23" is included.

validate.java

package com.example.IUBHQuiz;

import java.sql.*;


public class validate {
    public static boolean checkUser(String email,String pass)
    {
        boolean st =false;
        try {

            //loading drivers for mysql
            Class.forName("com.mysql.cj.jdbc.Driver");

            //creating connection with the database
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quizdb","root","1234");
            PreparedStatement ps = con.prepareStatement("select * from quizdb.users_db where username=? and pass=?");
            ps.setString(1, email);
            ps.setString(2, pass);
            System.out.println(email + pass);
            ResultSet rs =ps.executeQuery();
            st = rs.next();

        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return st;
    }

}

in the pom.xml I've added

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.23</version>
</dependency>

Edit: I'm using IntelliJ as the IDE and add the library through Project Settings -> Libraries -> + -> From Maven -> Screenshot of the project Tree

And the promised pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>IUBHQuiz</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>IUBHQuiz</name>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.7.0</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.0</version>
            </plugin>
        </plugins>
    </build>
</project>

Thank you for helping me. Janick




Aucun commentaire:

Enregistrer un commentaire