I am developing a dynamic web project in java with eclipse using the tomcat server.

I have created a database that I want to connect to my web application. For this purpose, I installed the jdbc jar in the classpath but when I open the tomcat server to test my application, I get a classNotFoundException.

What I don't understand is that it's only when I run the application in the server that this error occurs. I did some testing locally to see if the problem was with the jar but no, running as a java application is fine.

Doing some research, I saw that there was talk of DBCP and configuration in tomcat files. But I am literally lost. Does this mean that the jdbc jar is not needed? Why and when should we use DNCP instead of driver manager? What should I do to connect my application to my database?

I am attaching below the structure of my code and the error I am experiencing. Thanks for your help.

This is my Connect class

package fr.dw2.server;

import java.sql.*;

public class Connect {
    private PreparedStatement ins;
    private PreparedStatement sel;
    private PreparedStatement sel2;
    private Connection c;
    
    /**
     * Constructor
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws java.io.IOException
     */
    public Connect(/*Connection db*/) throws SQLException, ClassNotFoundException, java.io.IOException {
        //System.out.println("yiojjjka");
        // load JDBC pilote
        Class.forName("com.mysql.jdbc.Driver");
        
        System.out.println("yiojjjkam1");
        // registration on driver manager
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());

        System.out.println("yiojjjkam");
        // connection to the database
        c = DriverManager.getConnection("jdbc:mysql://localhost:3305/users", "root", "");
    
        // c = db;
        // statement zone
        sel = c.prepareStatement("SELECT name,email " + "FROM users "+ " WHERE password = ?");
        ins = c.prepareStatement("INSERT INTO users(name,email,password) " + "VALUES (?,?,?)");
        sel2 = c.prepareStatement("SELECT ? " + "FROM users");
        System.out.println("yiojjjka");
    }
    
    /**
     * 
     * @param id - identifier of the user
     * @param password - password of the user
     * @return true if a user is registered in the database and false if not
     */
    public boolean verifyUser(String id, String password) throws SQLException {
        sel.setString(1, password);
        ResultSet res = sel.executeQuery();
        boolean registered = false;
        while(!registered & res.next()) {
            if(res.getString("name") == id || res.getString("email") == id)
                registered = true;
        }
        return registered;
    }
    
    /**
     * 
     * @param name
     * @param email
     * @param pwd
     * @return string - message if the registration succeed
     * @throws SQLException
     */
    public String insertUser(String name, String email, String pwd) throws SQLException {
        ResultSet res;
        sel2.setString(1, name);
        res = sel2.executeQuery();
        if(res.next())
            return "Nom déjà utilisé, choisissez-en un autre";
        sel2.setString(1, email);
        res = sel2.executeQuery();
        if(res.next())
            return "Email déjà utilisé, choisissez-en un autre";
        ins.setString(1, name);
        ins.setString(2, email);
        ins.setString(3, pwd);
        ins.executeUpdate();
        return "OK";
    }
    
    /**
     * Close the connection
     * @throws SQLException
     */
    public void close() throws SQLException {
        ins.close();
        sel.close();
        sel2.close();
        c.close();
    }
}

And this is the way I call it in my doPost method

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // register the id and the password in variables for verification in the database
        String id = request.getParameter("id");
        String pwd = request.getParameter("pwd");
        Connect c;
        
        // remove leading and ending space in the password and id
        id = id.trim();
        pwd = pwd.trim();
        System.out.println("ya");
        if(id.length()>0 & pwd.length()>0) {
            System.out.println("yam");
            // will help later creation of the bean
            request.setAttribute("id", id);
            request.setAttribute("pwd", pwd);
            
            // checks with the jdbc if the class exists
            try {
                System.out.println("yioa");
                 c = new Connect();
                System.out.println("yioma");
                if(c.verifyUser(id, pwd)) {
                    System.out.println("OK!");
                    request.setAttribute("error", "Erreur, identifiant ou mot de passe invalide");
                    getServletContext().getRequestDispatcher("/WEB-INF/connect.jsp").forward(request, response);
                }
                else {
                    System.out.println("nope");
                    request.setAttribute("error", "Erreur, identifiant ou mot de passe invalide");
                    getServletContext().getRequestDispatcher("/WEB-INF/connect.jsp").forward(request, response);
                }
                System.out.println("Yo");
                c.close();
            } catch (SQLException | ServletException | IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        // redirects to the connection page
        else {
            request.setAttribute("error", "Erreur, vous devez remplir tous les champs");
            getServletContext().getRequestDispatcher("/WEB-INF/connect.jsp").forward(request, response);
        }
        
    }
0

There are 0 answers