hey there,
I am trying to make my first connection to a mysql server with java. I took the following code from another thread:

import java.sql.Connection;
   import java.sql.DriverManager;
     
   public class Connect {
      public static Connection ConnectDB()
      {
         Connection conn = null;
      
         try
         {
            String userName = "root";
            String password = "";
            String url = "jdbc:mysql://localhost:3306/test";
            Class.forName ("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
         }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         return (conn);
      }
		
		public static void main(String[] args){
			ConnectDB();
		}
		
   }

it compiles well, but when I run the code, I cat the following complaint:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Connect.ConnectDB(Connect.java:16)
at Connect.main(Connect.java:28)

how do I go about making the com.mysql.jdbc.Driver available. is it something I need to download and install separately from JDK or is it something that comes with JDK but I am not knowing how to use. please help!

Yah /**com.mysql.jdbc.Driver**/ is an exception you get if there are no mySql driver configured:
download mysql driver type 4, copy and paste the .jar file as the vaiable name below specifies. go to my computer and right click properties
, check enviroment variable and click new.

Variable name : CLASSPATH
Variable value : C:\Program Files\Java\jdk1.5.0_17\jre\lib\ext\mysql-connector-java-5.1.7-bin.jar

research furthermore on configuring mysql driver, ive given you 95% hint.

hmmm, just my interest about ...

a few Topics back I declared that

driver type 4

is implemented in Java6, and works with some latest MySql driver running on Java 1.5...

thanks for replying guys. I have fixed this issue on my windows machine, but can't seem to do so on my ubuntu machine. thanks, anyways.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.