PostgreSQL JDBC驱动程序不适用于Heroku数据库连接 [英] PostgreSQL JDBC Driver not working for Heroku DB Connection
问题描述
公共类ConnectionFactory {
$ b我有一个类试图连接到Heroku数据库$ b public Connection getConnection(){
System.out.println(Conectando ao banco);
尝试{
返回DriverManager.getConnection(connectionstring,username,password);
} catch(SQLException e){
抛出new RuntimeException(e);
$ b 它返回的是
java.lang.RuntimeException:java.sql.SQLException:
找不到合适的驱动程序
jdbc:postgres: //osnvehqhufnxzr:TS3Qt37c_HHbGRNKw3yk7g88fp@ec2-54-225-93-34.compute-1.amazonaws.com:5432/d39mfq0odt56bv
我已经在项目的构建路径中尝试过 postgresql-9.3-1103.jdbc3.jar 和 postgresql-9.4.1209.jre6.jar
。我做错了什么?
解决方案解决方案很简单, System.getenv(JDBC_DATABASE_URL)返回它在服务器端,它做了正确的登录和密码配置。
public Connection getConnection()throws URISyntaxException,SQLException
{
String urlDB = System.getenv(JDBC_DATABASE_URL);
返回DriverManager.getConnection(urlDB);
}
I have a class that tries to connect to a Heroku database:
public class ConnectionFactory {
public Connection getConnection() {
System.out.println("Conectando ao banco");
try {
return DriverManager.getConnection("connectionstring", "username", "password");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
What it returns is:
java.lang.RuntimeException: java.sql.SQLException: No suitable driver
found for
jdbc:postgres://osnvehqhufnxzr:TS3Qt37c_HHbGRNKw3yk7g88fp@ec2-54-225-93-34.compute-1.amazonaws.com:5432/d39mfq0odt56bv
I already tried postgresql-9.3-1103.jdbc3.jar and postgresql-9.4.1209.jre6.jar
in the Build Path of the project. What I am doing wrong?
解决方案 The solution was simple, System.getenv("JDBC_DATABASE_URL") returns it in server and it does the correct configuration of login and password.
public Connection getConnection() throws URISyntaxException, SQLException
{
String urlDB = System.getenv("JDBC_DATABASE_URL");
return DriverManager.getConnection(urlDB);
}
这篇关于PostgreSQL JDBC驱动程序不适用于Heroku数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!