Lars Kakavandi-Nielsen
Lars Kakavandi-Nielsen

Reputation: 2198

com.mysql.jdbc.Driver fails to compile for JSP

I am trying to make a logIn system with JSP and connect to a MySQL database, and get the following error: Error message

My code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="com.core.bibit.service.Security"
import="javax.servlet.*"
import="java.util.*"
import="com.mysql.jdbc.Driver"
%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");

boolean test = new Security().validateUser(username, password);
System.out.println("Authen: " + test);
if(test){
    response.sendRedirect("test.jsp");
}else{
    response.sendRedirect("about.jsp");
} 
%>

and

package com.core.bibit.service;
import com.mysql.jdbc.Driver;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Security {

    private final static int ITERATION_NUMBER = 1000;

    public Security(){

    }

    ...

    public boolean validateUser(String userName, String password){

        boolean valid = false;

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:8889/bibit?"
                                                             + "user=root&password=root");                  

            String query = "SELECT * FROM bibit.User WHERE username = ?";

            ps = con.prepareStatement(query);
            ps.setString(1, userName);

            rs = ps.executeQuery();

            String digest, salt;

            if(rs.next()){
                digest = rs.getString("PASSWORD");
                salt = rs.getString("SALT");

                System.out.println("Digest: " + digest);
                System.out.println("Salt: " + salt);

                if(digest == null || salt == null){
                    System.out.println("IF1");
                    throw new SQLException("Database inconsistant Salt or Digested Password altered");
                }
                if(rs.next()){
                    System.out.println("IF2");
                    throw new SQLException("Database inconsistent two CREDENTIALS with the same LOGIN");
                }
                /*else{
                    System.out.println("IF3");
                    digest = "000000000000000000000000000=";
                    salt = "00000000000=";
                }*/

                byte[] bDigest = base64ToByte(digest);
                byte[] bSalt = base64ToByte(salt);

                //compute digest
                byte[] proposedDigest = getHash(ITERATION_NUMBER, password, bSalt);
                System.out.println("bDigest: " + bDigest);
                System.out.println("proDigest: " + proposedDigest);

                System.out.println("Response: " +     Arrays.equals(proposedDigest, bDigest)); // && valid)
                valid = Arrays.equals(proposedDigest, bDigest);
            }




        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
            //  rs.close();
                //ps.close();
                //con.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return valid;

        }
    }

}

I use TomEE / Tomcat 7.0 and my MySQL library is: mysql-connector-java-5.1, and I have tried to add this to the server.xml file:

<Resource name="jdbc/DATABASENAME" type="javax.sql.DataSource" maxActive="4" maxIdle="2"    username="root" password="" maxWait="23000" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:8889/bibit"></Resource>

Can anyone explain what is wrong and perhaps how to fix it or just a push in the right direction?

SOLUTION: add the mysql connector through Eclipse if one start the sever through there and import files in one line as shown below

Upvotes: 0

Views: 1362

Answers (2)

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12983

Your JSP code doesn't need all those imports except Security class, remove all other

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.core.bibit.service.Security" %> //only Security class is needed

Check the method getConnection

public static Connection getConnection(String url, 
                                       String user, 
                                       String password) throws SQLException

Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.

Parameters:

  • url - a database url of the form jdbc:subprotocol:subname
  • user - the database user on whose behalf the connection is being made
  • password - the user's password

Make sure all the required jars are in classpath

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240860

You need to use comma separated import list for example

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.core.bibit.service.Security, javax.servlet.*,java.util.*, com.mysql.jdbc.Driver"%>

and make sure you have required jars/classes in classpath (during runtime as well)

Upvotes: 2

Related Questions