0

I'm trying to run this program

import java.sql.*;
import java.io.*;

public class FirstExample
{
     // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/EMP";

    //  Database credentials
    static final String USER = "root";
    static final String PASS = "pass";

    public static void main(String[] args) 
    {
        Connection conn = null;
        Statement stmt = null;
        try
            {
                //STEP 2: Register JDBC driver
                Class.forName("com.mysql.jdbc.Driver");

                //STEP 3: Open a connection
                System.out.println("Connecting to database...");
                conn = DriverManager.getConnection(DB_URL,USER,PASS);

                //STEP 4: Execute a query
                System.out.println("Creating statement...");
                stmt = conn.createStatement();
                String sql;
                sql = "SELECT title,duration,protocol,URL,thumbURL,favorite FROM Videos";
                ResultSet rs = stmt.executeQuery(sql);

                //STEP 5: Extract data from result set
                while(rs.next())
                {
                    //Retrieve by column name
                    String title = rs.getString("title");
                    String duration = rs.getString("duration");
                    String protocol = rs.getString("protocol");
                    String URL = rs.getString("URL");
                    String thumbURL = rs.getString("thumbURL");
                    String favorite = rs.getString("favorite");

                    //Display
                    System.out.println("Title:" + title);
                    System.out.println("Duration:" + duration);
                    System.out.println("Protocol:" + protocol);
                    System.out.println("URL:" + URL);
                    System.out.println("ThumbURL:" + thumbURL);
                    System.out.println("Favorite:" + favorite);
                }

                //STEP 6: Clean-up environment
                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 se2)
                {
                }// nothing we can do
                try
                {
                    if(conn!=null)
                    conn.close();
                }
                catch(SQLException se)
                {
                    se.printStackTrace();
                }//end finally try
            }
        System.out.println("Goodbye!");
        }
    }

But I'm getting the ClassNotFoundException

D:\XML Tests>javac FirstExample.java

D:\XML Tests>java FirstExample
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at FirstExample.main(FirstExample.java:21)
Goodbye!

Having consulted the numerous question available i.e setting the PATH system variable to the Connector directory, it still isnt working. Any help please?

0

3 Answers 3

2

Add the jar containing the mysql driver class com.mysql.jdbc.Driver in the classpath.

10
  • 1
    I've already done that. Specified C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.28-bin.jar in Path system variable Commented Feb 17, 2014 at 10:41
  • It should be there in the java classpath and not in system path. Commented Feb 17, 2014 at 10:42
  • Set the jar in java classpath or run the code using java -cp mysql-connector-java-5.1.25-bin.jar FirstExample Commented Feb 17, 2014 at 10:47
  • Doing that it says "Error: Could not find or load main class FirstExample" Commented Feb 17, 2014 at 10:54
  • @user2464622 do you compile the code ? U able to c FirstExample.class file ?? Commented Feb 17, 2014 at 10:55
0

Add MySQL driver jar to your project classpath and done.

0

The error is trying to tell you that it's not able to find the com.mysql.jdbc.Driver which is required when you are using MySql for data storage. So What you need to do is download the JConnector. And then import that jar file into your project classpath. Then you will not get the error.

2
  • I'm already using that..is it a problem of it not detecting the driver? Commented Feb 17, 2014 at 10:54
  • Have you included it in the classpath of project ? Commented Feb 17, 2014 at 11:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.