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!
source share