0
votes

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.

2
I see spaces in your path. Can that be the issue?Sourav 'Abhi' Mitra
@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.learner
You shall not be getting error like you posted, java.lang.ClassNotFoundException: com.sql.jdbc.Driver .Michal
@Michal I didn't get you. I have copied the error to prevent mistake while typing.learner
You do Class.forName("com.mysql.jdbc.Driver"); Therefore the error has to be java.lang.ClassNotFoundException: com.mysql.jdbc.DriverMichal

2 Answers

1
votes

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.

1
votes

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.