Runtime Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I am new to mysql and jdbc and I am getting error in this header. I searched all day and cannot find a solution that works for me.

What I tried: uninstall / reinstall mysql, copy paste mysql-connector-java-5.1.25-bin.jar and ojdbc7.jar to the same place as the .class file that I am trying to run, rebuild the program in another catalog and maybe a couple other things.

I use notepad ++ for encoding and the Windows command line to compile and run. It compiles fine, but I'm trying to work with

C: \ Projects \ bin> java -cp. ClientBase

Output:

java.lang.ClassnNotFoundException: com.mysql.jdbc.Driver

at java.net.URLClassloader $ 1.run (URLClassLoader.java data36)
at java.net.URLClassLoader $ 1.run (URLClassLoader.javahaps55)
in java.security.AccessController.doPrivileged (Native method)
at java.net.URLClassLoader.findClass (URLClassLoader.javahaps54)
in java.lang.ClassLoader.loadClass (ClassLoader.java:432)
at sun.misc.Launcher $ AppClassLoader.loadClass (Launcher.java:308)
in java.lang.ClassLoader.loadClass (ClassLoader.javahaps56)
in java.lang.Class.forName0 (Native method)
in java.lang.Class.forName (class .java: 188)
in ClientBase.main (ClientBase.java21)
Bye.

// import packages import java.sql.*; // create class ClientBase public class ClientBase{ // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/CLIENTBASE"; // Database credentials static final String USER = "root"; static final String PASS = ""; // Begin method main public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ // register JDBC driver Class.forName("com.mysql.jdbc.Driver"); // Open connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, name, address, address 2, city, phone, state, zip, fax FROM CLIENTBASE"; ResultSet rs = stmt.executeQuery(sql); // Extract data from result set while(rs.next()){ // Retrieve by column name int id = rs.getInt("id"); String name = rs.getString("name"); String address = rs.getString("address"); String address2 = rs.getString("address2"); String city = rs.getString("city"); String phone = rs.getString("phone"); String state = rs.getString("state"); String zip = rs.getString("zip"); String fax = rs.getString("fax"); // Display values System.out.print("ID: " + id); System.out.print(" Name: " + name); System.out.println("Address:" + address); System.out.println(address2); System.out.print("City:" + city); System.out.print(" State: " + state); System.out.println(" Zip: " + zip); System.out.print("Phone: " + phone); System.out.println(" Fax: " + fax); } // end while // clean up rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ // Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ // Handle errors for Class.forName e.printStackTrace(); }finally{ // finally block used to close resources try{ if(stmt!=null) stmt.close(); }catch(SQLException se){ se.printStackTrace(); } // end finally } // end try System.out.println("Goodbye."); } // End method main } // end class ClientBase 

I must also say that I am going to an online tutorial for this code. This is not quite the way they have, since I decided to do something a little different from them, but it is usually the same. I do not think this is a problem with the code, although due to an error.

Any help would be appreciated! I'm going crazy!

+4
source share
6 answers

You need to add the connector library to the path of the Runtime class:

 java -cp .;mysql-connector-java-5.1.25-bin.jar ClientBase 

My example uses the classpath delimiter for Windows ";" , on other systems it may be different ( ":" on Linux / Mac). It also assumes that mysql-connector-java-5.1.25-bin.jar is in the same folder. If not, provide a library path instead of a simple name.

ClientBase means Java class file name here

 c:\>javac Test.java c:\>java -cp .;F:\CK\JavaTest\JDBCTutorial\mysql-connector-java-5.1.18-bin Test 
+11
source

If you are using netbeans, do the following 1. Open the Netbeans IDE 2. Right-click your project. 3. Select Properties. 4. On the left side, click "Libraries." 5. On the Compile tab, click the Add Jar / Folder button. 6. Select the downloaded file "mysql-connector-java-5.1.25-bin.jar" (download Connector / J from dev.mysql.com) 7. Click OK. Run it again ... Its work.

+2
source

Add mysql.connector-java-xxx-bin.jar to your libraries folder. No need to import anything. Have a good one.

+1
source

I was also get same problem :-
As I decided for the Linux system .

1.) Download the mysql-connector-java-5.1.18-bin.jar file from the specified link or another.

2.) Then paste it into the same folder or directory of your class or file (where you want to establish a connection)

3.) Now use the following command on the linux command prompt.

java -cp .:mysql-connector-java-5.1.18-bin.jar YourFileName ..

That is all you need to do ... :)

+1
source

What did you import? From the documentation: http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html

 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; // Notice, do not import com.mysql.jdbc.* // or you will have problems! 

Comment:

Why are you using notepad ++? install the IDE (Eclipse / Netbeans / IntelliJ) - it will be much easier to find such problems (for example, banks not included)

0
source

If you use Swing , you need to add External Jar my-sql-connect

else if you use jsp servlet you need to add an external banner and copy this Jar to the project folder WEB_INF / lib

0
source

Source: https://habr.com/ru/post/1496279/


All Articles