Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Class not Found Prob with JDBC

Zulfi KhanJul 20 2017 — edited Jul 20 2017

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.

This post has been answered by Zulfi Khan on Jul 20 2017
Jump to Answer

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 17 2017
Added on Jul 20 2017
1 comment
1,537 views