0

I use NetBeans IDE 12.5 and have Java 17. I use macOS big sur 11.1. I have downloaded the jar file of the Derby database and also developed the connection and a database too, yet it is showing this error again and again. I even downloaded GlassFish. My code is:

 import java.sql.*;

 public class Main {
    public static void main(String args[])
    {
    try
    {
        Class.forName("org.apache.derby.jdbc.ClientDriver"); //driver for derby
        Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/Students","root",""); 
            Statement stat=conn.createStatement();
            ResultSet rs=stat.executeQuery("select * from STUD");
            while(rs.next())
            {
                //System.out.print(rs.getInt(1));
                System.out.println(rs.getInt(1)+" "+rs.getString(2));
            }
        }
        catch(ClassNotFoundException | SQLException e)
        {
            System.out.println(e);
        }
     }
  }

Error:

--- exec-maven-plugin:3.0.0:exec (default-cli) @ Vishu_DB ---
java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver

I'm new to this and I don't know how to get rid of this error.

Edit 1:I'm adding the pom.xml code as well

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>Ananya_DB</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <name>Ananya_DB</name>
</project>

Unfortunately I am unable to add any dependencies in this file. If I try and add, it gives me error.

Vishu
  • 1
  • 1
  • The comment in your code is incorrect :-) – Stephen C Oct 19 '21 at 10:56
  • 1
    You have downloaded the JAR file, but you don't tell how you have included it in your project. – Robin Oct 19 '21 at 10:59
  • 1
    You shouldn't use `Class.forName` to load the driver. `DriverManager` does it for you. But in this case, you probably don't have the JAR file containing the driver on the runtime classpath. (And you don't need Glassfish for this.) – Stephen C Oct 19 '21 at 10:59
  • But if you insist on using `Class.forName`, you need to use the correct class name for the Derby driver ... which depends on the JAR file. It could be `org.apache.derby.jdbc.ClientDriver` or `org.apache.derby.jdbc.EmbeddedDriver` or potentially something else. Which is why it is a *bad idea* to do it that way! (Irrespective of what out-of-date nonsense you found in someone's blog.) – Stephen C Oct 19 '21 at 12:21
  • Can you show your `pom.xml` dependencies? – asbachb Oct 19 '21 at 22:01
  • @StephenC I corrected my comment. Thanks for pointing out. When i am changing the class name it's showing the error main class not found. – Vishu Oct 20 '21 at 06:43
  • Sounds like you have messed up the classpath. See https://stackoverflow.com/a/18093929/139985 – Stephen C Oct 20 '21 at 07:46
  • @StephenC Thank you so much for the link, I was able to change the class name but unfortunately I am still getting the error java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver. – Vishu Oct 20 '21 at 15:15
  • @asbachb I have edited my post and added the pom.xml file content. Whenever I'm trying to add the dependency, it's giving me error. Also I'm not very well accustomed with this dependency concepts and all. If possible can you please tell me what to add to the above code. – Vishu Oct 20 '21 at 15:23
  • Basically ... are going to have to stop just copying and pasting solutions ... and start **understanding** what people are telling you. As people have said, you don't need to call `Class.forName`. Indeed, that call is (indirectly) the cause of your problem. Just remove it entirely. – Stephen C Oct 20 '21 at 15:46
  • @StephenC Sir I removed that entirely yet java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/Students , this error got generated. I understood that indirect call is not needed, I forgot to edit my post by removing that line.I'm sorry for the inconvenience caused because of my many edits and comments. – Vishu Oct 20 '21 at 15:58
  • Thank you everyone for your help. The problem is resolved. I just added the jar file in the project and it worked. – Vishu Oct 21 '21 at 06:29

0 Answers0