2

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;
}      
}
}
user3615401
  • 21
  • 1
  • 1
  • 4

9 Answers9

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
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"
Fhamtaom
  • 96
  • 1
  • 3
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.

rpirez
  • 431
  • 2
  • 8
  • 20
  • ok sorry for late reply i could not connect the net. wait i will check it and say... – user3615401 Jul 02 '14 at 00:05
  • 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.... – user3615401 Jul 02 '14 at 00:17
  • 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. – rpirez Jul 02 '14 at 08:06
  • now what i have to proceed. if you know could you tell me. – user3615401 Jul 02 '14 at 08:21
  • why you want to run .Jar from desktop? – rpirez Jul 02 '14 at 09:56
  • if we want to give our project to any office. have to change that to exe file. to change that jar as exe file have to take separately and put in third party tool afterwards only given to office na. – user3615401 Jul 02 '14 at 10:18
  • The only solution I know is to transform this .Jar in an application, ie, create an installer of your application, and then you can use the application where you want. But your database is not localhost? – rpirez Jul 02 '14 at 11:07
  • See this tutorial: It helps you. http://www.youtube.com/watch?v=G8ccin6w4JM&index=114&list=PLB04B4E5D9B58C13D – rpirez Jul 02 '14 at 11:09
  • actually now only i came to online. i just downloading that video now. not yet starts to work – user3615401 Jul 02 '14 at 15:57
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

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

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

SparkOn
  • 8,806
  • 4
  • 29
  • 34
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.

naeem1098
  • 123
  • 2
  • 9
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.

MosesK
  • 359
  • 3
  • 7
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

0

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
Sergio Perez
  • 571
  • 6
  • 6