0

Error is: java: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown and occurs at this: Class.forName("org.postgresql.Driver");

Most of the time, it was weird maven formatting or the such, but I think I have it right (copy pasted). Here is the pom.xml

<?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>org.example</groupId>
    <artifactId>Database-Test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1203-jdbc4</version>
        </dependency>
    </dependencies>
</project>

If needed, here is the file path enter image description here

and the main class

import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;

public class DBTEST{
    public static void main (String[]args) throws SQLException, URISyntaxException{
        System.out.println(getConnection());
    }
    private static Connection getConnection() throws URISyntaxException, SQLException{
        Class.forName("org.postgresql.Driver");
        //String dbUrl = System.getenv("DATABASE_URL");
        String dbUrl = "postgres://fxeymwfokhsimv:bd5c9975533c20ed3ab4df7c0ea263911207854c2cac46983fafce18689a1161@ec2-184-72-162-198.compute-1.amazonaws.com:5432/dlfvrduvj5qcr";
        return DriverManager.getConnection(dbUrl);
    }
}

DB Host and Databaseenter image description here Don't worry about me sharing my database credentials - this is a test project and is just for me to figure out how to use it.

1 Answers1

0

Maven won't package dependencies with jar by default, you have to do it with the plugin. Check this thread - Including dependencies in a jar with Maven

You don't need Class.forName as you are using jdbc 4 driver. The error in your program is due to unhandled ClassNotFoundException for using Class.forName and invalid driver URL.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Yuvaraj G
  • 1,157
  • 9
  • 17
  • Would this also apply to running with Intellij? The problem exists there as well, so if it doesn't, then it might not just be that. –  Sep 08 '20 at 16:58
  • You have to throw or handle `ClassNotFoundException` from your getConnection() method, I think something wrong with your syntax check in the IDE. This code in eclipse alerts me to handle` ClassNotFoundException` on using `Class.forName` – Yuvaraj G Sep 08 '20 at 17:06
  • Adding a throws ClassNotFoundExcpetion results in `Exception in thread "main" java.sql.SQLException: No suitable driver found for postgres://fxeymwfokhsimv:bd5c9975533c20ed3ab4df7c0ea263911207854c2cac46983fafce18689a1161@ec2-184-72-162-198.compute-1.amazonaws.com:5432/dlfvrduvj5qcr` –  Sep 08 '20 at 17:11
  • The jdbc url goes like this `jdbc:postgresql://host/db`, you are missing the jdbc prefix and also postgres should be postgresql – Yuvaraj G Sep 08 '20 at 17:14
  • Not sure if using heroku would affect anything - but the DATABASE_URL config var is `postgres://tzfelalpgpknzs:99cce506d410d89c1ee51f2f8b35eae9c6faacd4b578c29ff18b5e5de3cfcf98@ec2-50-17-90-177.compute-1.amazonaws.com:5432/d7mfkbl2si37vm`. Is there any way to format this to look like this `jdbc:postgresql://host/db`? Also, it creates a new error, although this might just be because I'm running it locally isntead of on the Heroku server. Edit: running on Heroku produces this: `Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driver` –  Sep 08 '20 at 17:24
  • You can do some substring operations to strip the host and db from your original url and compose in `jdbc:postgresql://host/db` format. And about running it outside the IDE, as jar(which is causing `ClassNotFoundException` at runtime), requires packaging all the dependencies, check the link which I have shared in the answer. – Yuvaraj G Sep 08 '20 at 17:42
  • I've updated the question with database credentials and ran it in the IDE. This error is produced: `Exception in thread "main" org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "66.215.146.204", user "hithi", database "d7mfkbl2si37vm", SSL off` I think I might be formatting it wrong, and the error is a bit weird. Are there some extra steps since the database is external? –  Sep 08 '20 at 18:02
  • Your code works fine, this error is because I think the database server is not configured to accept the requests from the host you are trying from. Try contacting the ppl responsible for your db server. – Yuvaraj G Sep 08 '20 at 18:20
  • Huh, ok I'll try that. Thanks for all your help! –  Sep 08 '20 at 18:22
  • This is a bit later, wrote the string stuff, but the jar file seems to not work even though I added the maven-assembly-plugin in the pom. Updated pom.xml in the question. –  Sep 08 '20 at 21:25