javajdbcderbynetbeans-8glassfish-4.1

Getting ClassNotFoundException while working on derby databse from Glassfish on Netbeans


This is the code I've written:

package JDBC;
import java.sql.*;
public class JDBC {
    public static void main(String[] args) {
        try{
            String driverName = "org.apache.derby.jdbc.ClientDriver";
            String dbURL = "jdbc:derby://localhost:1527/Student";
            String dbUser = "DV";
            String dbPass = "Pass";
            Class.forName(driverName);
            Connection con = DriverManager.getConnection(dbURL, dbUser, dbPass);
            Statement stmt = con.createStatement();
            String sql;
            sql = "create table Student(Enroll_No Varchar(20), Name Varchar(20), Gender Char, Age Int, CGPA Float, Email_ID Varchar(30))";
            stmt.executeUpdate(sql);
            stmt.close();
            con.close();
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}

The error I'm getting is:

run:
java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
BUILD SUCCESSFUL (total time: 0 seconds)

and no table is created. I'm using Glassfish 4.1.1(Derby Database) on NetBeans IDE 8.2


Solution

  • For a ClassNotFoundException, you might want to check about missing .jar files in your project library. Go to your Project Folder on Netbeans (top left corner), then Libraries and then check for "derbyclient.jar" and "derby.jar" files. If they are not present, add them manually from your GlassFish folder. Right click on Libraries -> Add JAR/Folder... Browse to your Glassfish 4.1.1 installation folder -> javadb -> lib and select derbyclient.jar and derby.jar files. This should work and the exception shall be removed.