ClassNotFoundException, heroku postgresql

huangapple go评论80阅读模式
英文:

ClassNotFoundException, heroku postgresql

问题

问题出在:

class.forName("org.postgresql.Driver")

pom.xml 中有以下依赖:

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1203-jdbc4</version>
        </dependency>

错误为:

java: 未报告的异常 java.lang.ClassNotFoundException; 必须捕获或声明为抛出

这应该是所有重要内容。如果需要更多内容来解决问题,以下是完整信息。(我不在意是否公开数据库,因为它只是一个测试。)

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>
                <groupId>com.heroku.sdk</groupId>
                <artifactId>heroku-maven-plugin</artifactId>
                <version>2.0.6</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1203-jdbc4</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
</project>

Main Class

import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

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);
    }
}

文件路径

ClassNotFoundException, heroku postgresql

英文:

The issue is at

class.forName("org.postgresql.Driver")

pom.xml has this dependency:

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1203-jdbc4</version>
        </dependency>

The error is

java: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown

That should be all the important stuff. If more stuff is needed for figuring stuff out, heres all of it. (dont really care if i release the database it'll be deleted since its just a test)

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>
                <groupId>com.heroku.sdk</groupId>
                <artifactId>heroku-maven-plugin</artifactId>
                <version>2.0.6</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1203-jdbc4</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
</project>

Main Class

import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

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);
    }
}

FILE PATH

ClassNotFoundException, heroku postgresql

答案1

得分: 0

请检查您在Maven中指定的驱动程序版本和AWS实例中的版本是否相同。

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1203-jdbc4</version>
    </dependency>
英文:

Check the version of driver you have specified in your maven and the aws instance is same.

    &lt;dependency&gt;
        &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
        &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
        &lt;version&gt;9.4-1203-jdbc4&lt;/version&gt;
    &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年9月8日 09:33:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63786038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定