Forum Stats

  • 3,727,656 Users
  • 2,245,433 Discussions
  • 7,852,906 Comments

Discussions

Class not Found Prob with JDBC

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited July 2017 in New To Java

Hi,

I am getting this problem. But this time, i have set the CLASSPATH variable correctly.

>java FirstExample

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

        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

        at java.lang.Class.forName0(Native Method)

        at java.lang.Class.forName(Class.java:264)

        at FirstExample.main(FirstExample.java:31)

Goodbye!

>echo %CLASSPATH%

D:\download\mysql-connector-java-5.0.8-bin.jar;.

Code is little bit changed:

import java.sql.*;

import javax.swing.*;

import java.util.ArrayList;

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/ebooksshop";

   //  Database credentials

   static final String USER = "root";

   static final String PASS = "";

   static ArrayList<String> items = new ArrayList<String> ( );

   String sql="";

   ResultSet rs= null;

   static int recordCnter=0;

   static String id="";

   static String title="";

   static String author= "";

   static String price="";

   static String quantity ="";

   static String pages = "";

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 * FROM books";

      ResultSet rs = stmt.executeQuery(sql);

     

      //STEP 5: Extract data from result set

      while(rs.next()){

         //Retrieve by column name

        

          id = rs.getString("id");

          title = " " + rs.getString("title");

          author   = " " + rs.getString("author");

          price = " " + rs.getString("price");

          quantity = " " + rs.getString("qty");

          pages = " " + rs.getString("pages");

          String record = id +  price + quantity + pages;

          items.add(record);

         //Display values

         //System.out.print("name: " + name);

         //System.out.print(", owner: " + owner);

        

      }     

      String strData="";

      String[] fieldValue;

      while (recordCnter < items.size( )){

                strData=items.get(recordCnter);

                fieldValue=strData.split("\\s+");

                JOptionPane.showMessageDialog(null,fieldValue);

       

        recordCnter++;

      }              

      //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

   }//end try

   System.out.println("Goodbye!");

}//end main

}//end FirstExample

Somebody please guide me.

Zulfi.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited July 2017 Accepted Answer

    Hi,

    This problem has been solved now. I was actually using wrong value for CLASSPATh. The correct values is:

    D:\download_nz\mysql-connector-java-5.0.8-bin.jar;.

    Zulfi.

Answers

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited July 2017 Accepted Answer

    Hi,

    This problem has been solved now. I was actually using wrong value for CLASSPATh. The correct values is:

    D:\download_nz\mysql-connector-java-5.0.8-bin.jar;.

    Zulfi.

This discussion has been closed.