3

i did project in netbeans 7.3.1 version and converted as jar if i run the jar from the dist folder means its work properly. if i copy and paste in desktop and run means it shows java.lang.classnotfoundexception:com.mysql.jdbc driver message will show. i could not find exactly. why it shows like that and what is the solution for it. please help me. am new to this netbeans... but added the mysql connector jar file in project. but it shows that error. please help me here is my code

package aam;
import java.sql.*;
import javax.swing.*;
public class Connect {
Connection con=null;
public static Connection ConnectDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tnpcb","root","");      
return con;

}
catch(ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null, e);
return null;
}      
}
}
2
  • possible duplicate of Adding jdbc driver to classpath Commented Jun 30, 2014 at 12:41
  • how to add jdbc driver to class path in netbeans Commented Jun 30, 2014 at 12:50

9 Answers 9

13
  1. Right click on Libraries > Click on Add Library.
  2. Scroll down to find MySQL JDBC driver.
  3. Press Shift + F11. (Clean and Build)
  4. Run
Sign up to request clarification or add additional context in comments.

1 Comment

Straight, slick and nifty
5

Setting the classpath :

  1. Download the JDBC MySQL Driver : https://dev.mysql.com/downloads/connector/j/
  2. Copy file in subfolder of project (ex: libs) (not 'src')
  3. Project->Properties->Libraries->Add Jar/Folder->Select file

And packaging dependent librairies with relative path in 'dist' :

  1. Project->Build->Packaging->Check "Copy Dependent Librairies"
  2. "Clean and Build"

1 Comment

Works like charm, this guy saved my night. Hell yeah
1
  1. Right click in libraries
  2. Add Jar/Folder
  3. Add your mysql-connector-java-5.1.13-bin.jar
  4. right click in you project; click on "Clean and Build";
  5. After that go to the netbeans project folder;
  6. It creats a new folder called:"Dist" In this folder will have a file ProjectName.Jar (Does an executable of your application);
  7. In netbeans when you created the Jar a line appears in output and that you can copy and run in cmd.

If there is any error will appear and the Stacktrace code. And then we can better analyze the situation.

9 Comments

ok sorry for late reply i could not connect the net. wait i will check it and say...
i tried this are you said this line ah... java -jar "D:\project\aam\dist\aam.jar" if i run this line in cmd it run the jar file....
Because then it's okay. I've also had this problem. I could not paste. Jar on the desktop and run it. I had to install a software and create an installer of my application.
now what i have to proceed. if you know could you tell me.
why you want to run .Jar from desktop?
|
1

1/ Click libraries http://prntscr.com/pio7pr

2/ Add libraries : http://prntscr.com/pio80y

3/ Scroll down to find MySQL JDBC driver :http://prntscr.com/pio8e0

Comments

1

You can add driver to Pom:

  1. Edit Pom.xml
  2. Add the follow code between </properties> and </project>
<dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.24</version>
    </dependency>
</dependencies>
  1. Clean and Build
  2. Verify "mysql-connector-java.jar" into ypur project Dependencies

Comments

0

Include the dependent mysql-connector.jar in the classpath while running your application

2 Comments

how to include the mysql connector.jar in classpath while run that
refer this it will make you more clear
0

If you are using MySQL 8.0, then use this driver name: "com.mysql.cj.jdbc.Driver". First of all download the libraries from the MySQL website then include them into your project. It will work smoothly.

Comments

0

I was working on Linux and encountered same error even after adding the jar and safely connecting from Services > Database window.

If you have obtained the jar and are getting same error after testing the Connection from Services > Database Window and it reports back connection success most likely you have not added the Jar in your libaries.

Go to Windows > Project > MyExampleProject > Libraries

Then Right Click > Add JAR/Folder

Add you sql-connector.jar

Now your jar is part of the libraries, now run your connection script again.

Comments

0

I'had this problem, and i found a solution that worked for me, if you have a maven project you have to follow this steps in order to add a dependency:

1 - right click on the the "dependecies" foleder.

2 - click "add dependency"

3 - write this inside the window that is gonna open:

Values

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.