英文:
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);
}
}
文件路径
英文:
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
答案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.
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1203-jdbc4</version>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论