0

I'm trying to connect with mysql from java and these are my CLASSPATH variable values which I've set using GUI in windows(control panel->system->advanced system settings->environment variables).

.;.;.;.;.;C:\PROGRA~2\JMF21~1.1E\lib\sound.jar;C:\PROGRA~2\JMF21~1.1E\lib\jmf.jar;C:\PROGRA~2\JMF21~1.1E\lib;E:\Face_rec\FaceDetect-java\lib\facedetect-openimaj.jar;C:\Program Files (x86)\MySQL\Connector J 5.1.27\mysql-connector-java-5.1.27-bin.jar

The bolded one is the classpath that I'm dealing with. Even though all different paths are separated by semicolon and the mysql-connector-java-5.1.27-bin.jar has been added to CLASSPATH i'm getting this error when I run my code.

java.lang.ClassNotFoundException: com.sql.jdbc.Driver.

this is the part of the code. And I've imported,

import java.awt.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JTextField;
import javax.swing.ImageIcon;

ok.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                String test=jf1.getText();
                String testa=jf2.getText();
                String n;
                String m;
                try
                {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con=DriverManager.getConnection

("jdbc:mysql://localhost/authentication?"+"user=root&password=letmein");
                    PreparedStatement p=con.prepareStatement("Select * from authentication.admin where 

id Like '"+test+"%'");
                    ResultSet rs=p.executeQuery();
                        rs.next();
                        n=rs.getString("Username");
                        m=rs.getString("Password");
                        con.close();
                    if(n.equalsIgnoreCase(test) && m.equalsIgnoreCase(testa))
                    {
                            JOptionPane.showMessageDialog(null,"Your username and password is 

correct");
                    }
                    else
                    {
                            JOptionPane.showMessageDialog(null,"Your username or password is 

incorrect");
                    }

                }
                catch(Exception es)
                {
                    System.out.println(es);
                }

            }
        });

Update:I'm not using any IDE(Eclipse of netbeans). I'm using only command prompt. Please help me to solve this.

5
  • I see spaces in your path. Can that be the issue? Commented Nov 14, 2014 at 19:32
  • @Abhi I tried double quoting the whole path. Like "C:\Program Files (x86)\MySQL\Connector J 5.1.27\mysql-connector-java-5.1.27-bin.jar" but didn't work. Commented Nov 14, 2014 at 19:37
  • You shall not be getting error like you posted, java.lang.ClassNotFoundException: com.sql.jdbc.Driver . Commented Dec 27, 2014 at 15:29
  • @Michal I didn't get you. I have copied the error to prevent mistake while typing. Commented Dec 27, 2014 at 15:32
  • You do Class.forName("com.mysql.jdbc.Driver"); Therefore the error has to be java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Commented Dec 27, 2014 at 15:40

2 Answers 2

1

You should also put it into your project. Depends on build tool you are using, but if you just copy it into lib folder will work in most cases.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello Maksym I'm sorry for not mentioning. I'm not using any build tool. Just excecuting it in command prompt. Is there anyway I can do this through command prompt?
I didn't get lib part. Copy to which lib folder?
In your project should be the lib folder, you can put it just there .
1

You must add the jar in the Lib folder of your project too.

Steps:

  1. Open Command Prompt.
  2. Go to the path where you have created the project.

    For Ex: If you have created your project in C:\Workspace\Test(where Test is your project name)

  3. In Command Prompt, type C:> cd C:\Workspace\Test

  4. Now your command prompt would look like this C:\Workspace\Test>
  5. Type dir after Test, it would show you the directory structure of your Project. Check if you have any Lib folder in that structure.
  6. If you have a lib folder, type dir lib in the command prompt which would show you the list of jars available in your project.
  7. You can add mysql-connector-java-5.1.27-bin.jar in lib if you don't find it.

Let me know if this helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.