0

My program ever shows these errors when I execute it:

Exception in thread "main" java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
    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 Aula4.main(Aula4.java:13)
Picked up _JAVA_OPTIONS: -Xmx512M
C:\Users\mikae\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

My file Aula4


import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author mikae
 */
public class Aula4 {
    public static void main (String[] args) throws ClassNotFoundException, SQLException {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            
            String url = "jdbc:derby://localhost:1527/sistema_academico";
            
            String usuario = "app";
            String senha = "app";
            Connection conexao;
            Scanner ler = new Scanner(System.in);
            int id_aluno;
            double nota;
            String nome;
            int i;
            conexao = DriverManager.getConnection(url, usuario, senha);
            String sql = "INSERT INTO aluno VALUES (?, ?, ?)";
            PreparedStatement stm = conexao.prepareStatement(sql);
            ResultSet rs = stm.executeQuery();
            
            System.out.println("Connected!");
            for (i=0;i<4;i++) { 
                
            System.out.println("Welcome! Type your ID");
            id_aluno = ler.nextInt();
            
            System.out.println("Now, type your name");
            nome = ler.next();

            System.out.println("And finally, type your grade");
            nota = ler.nextDouble();
            
            String SQL_Update = "UPDATE aluno SET nota=? WHERE id_aluno=?";
            stm = conexao.prepareStatement(SQL_Update);
            stm.setLong(1, id_aluno);
            stm.setString(2, nome);
            stm.setBigDecimal(3, new BigDecimal(nota));
            
            int registros = stm.executeUpdate();
            
                        while ( rs.next()) {
                id_aluno = rs.getInt("id_aluno");
                nome= rs.getString("nome");
                nota = rs.getDouble("nota");
            }
            
            stm.close();
            }
            
        } catch (SQLException ex) {
            System.out.println("Connection failed!");
        } 
    }
}

I want to connect my Database. I've created my Database and still shows these errors and I'm using Netbeans IDE 8.2.

4
  • How do you run your application? Commented Mar 31, 2021 at 13:12
  • @talex In Netbeans using Shift+F6. Commented Mar 31, 2021 at 13:13
  • 1
    You need to add driver jar to classpath. I don't know how to do it in netbeans Commented Mar 31, 2021 at 13:14
  • @talex Alright, I've gonna try this. Commented Mar 31, 2021 at 13:15

1 Answer 1

1

You need to add the jdbc driver to your java application. If you have a maven project you can add it by adding the following depencency to you pom.xml:

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.15.2.0</version>
</dependency>

If you use Netbeans you can download the driver from https://repo1.maven.org/maven2/org/apache/derby/derby/10.15.2.0/ and add it to the poject by following the guide at https://www.foxinfotech.in/2019/03/how-to-add-external-jar-file-in-netbeans-project.html.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not using Maven. Can I do otherwise?
I have updated my answer with info about Netbeans.
I followed the steps and It still remains.
Alright, finally I did it! My connection has failed, but the error that happened was fixed. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.