Issue
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.
Solution
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.
Answered By - Kaj Hejer