javanetbeans-12

Netbeans project wouldn't give output


I am using netbeans 12 with jdk 17. I built and ran the following project, it shows as

org.apache.derby.jdbc.ClientDriver BUILD SUCCESSFUL (total time: 0 seconds)

But the output is not obtained, the code has no errors and this is the only file in the project.

Code:

public class Bookclass {

public static void main(String[] args) {
    // TODO code application logic here
    try{
        Scanner in=new Scanner(System.in);
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        int ch;
        do{
            System.out.println("1.Insert 2.Update 3.Delete 4.Display 5.Exit\nEnter your choice:");
            ch=in.nextInt();
            books b=new books();
            switch(ch){
                case 1:
                    b.insert();
                    break;
                case 2:
                    b.update();
                    break;
                case 3:
                    b.delete();
                    break;
                case 4:
                    b.display();
                    break;
                case 5:
                    System.exit(0);
                    break;
            }
        }while(ch!=5);
    }catch(Exception e){
        System.out.println(e.getMessage());
    }
}

}


Solution

  • This line is your issue:

    Class.forName("org.apache.derby.jdbc.ClientDriver");
    

    It is throwing an exception and causing your code to move to the catch block, print the message and then exit. We don't know exactly what the error is because you hide the exception, but you are most likely getting a java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver

    Edit your catch block to show the full error message like so.

    public static void main(String[] args) {
        try{
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            System.out.println("It worked!");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    

    If you are getting a ClassNotFoundException then make sure that the class/driver is on your classpath (There are many existing answers on StackOverflow or the web about how to resolve this).