Cannot connect to heroku PostgreSQL db - java

I will connect with a simple java programme to my heroku Postgres database. But if i try to connect i get an error. Can anyone explain me the problem or can solve this problem?
Thx :D
Code:
public static void main(String[] args) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
// ========> from heroku website
String url = "jdbc:postgresql://ec2-107-20-214-225.compute-1.amazonaws.com:5432/databasename";
Properties props = new Properties();
props.setProperty("user", "someusername");
props.setProperty("password", "somepassword");
props.setProperty("ssl", "true");
connection = DriverManager.getConnection(url, props);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}...
Output:
-------- PostgreSQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
Connection Failed! Check output console
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:225)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:138)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
...
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
...

Thanks Lukas Eklund!
My url should look like this:
String url = "jdbc:postgresql://ec2-107-20-214-225.compute-1.amazonaws.com:5432/databasename
?user=someusername&password=somepassword
&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory";

Add sslmode to the database url:
String url =
"jdbc:postgresql://ec2-107-20-214-225.compute-.amazonaws.com:5432/databasename?sslmode=require

I had the same problem but I had to use 2 settings to get it work:
String url = "jdbc:postgresql://ec2-107-20-214-225.compute-.amazonaws.com:5432/databasename?ssl=true&sslmode=require
ssl=true
and
sslmode=require

Related

azure databricks JDBC URI connection error during proxy

java.sql.SQLNonTransientConnectionException: [Simba]JDBC Connection Refused: [Simba]JDBC Required Connection Key(s): Host, Port; [Simba]JDBC Optional Connection Key(s): AllowSelfSignedCerts, AsyncExecPollInterval, AutomaticColumnRename, CAIssuedCertNamesMismatch, CatalogSchemaSwitch, ConnSchema, DecimalColumnScale, DefaultStringColumnLength, DelegationToken, DelegationUID, DnsResolver, DnsResolverArg, FastConnection, krbJAASFile, NonSSPs, PreparedMetaLimitZero, RowsFetchedPerBlock, ServerVersion, ServiceDiscoveryMode, SocketFactory, SocketFactoryArg, SocketTimeOut, SSLKeyStore, SSLKeyStorePwd, SSLTrustStore, SSLTrustStorePwd, StripCatalogName, UseCustomTypeCoercionMap, UseNativeQuery
URI : spark://xxxx.azuredatabricks.com:448/default;transportMode=http;ssl=1httpPath=xxxx;AuthMech=3;UseProxy=1;proxyHost=xxxx;proxyPort=3128;
Trying to connect azure databricks throuh proxy.
Code :
private void createConnectionUsernameAndPassword(Configuration configuration)
throws ConnectionException {
// simba driver
Driver driver = new Driver();
// before creating the connection
if (!driver.acceptsURL(getJDBCUri())) {
throw new ConnectionException(AzureDBErrorCode.INVALID_JDBC_URI);
}
try {
this.connection = driver.connect(getJDBCUri(), properties);
} catch (SQLException e) {
throw new ConnectionException(e);
}
}
If you look into PDF documentation bundled with the driver you can see that it should be ProxyHost and ProxyPort (capitalized), while you have proxyHost and proxyPort. From the documentation (page 45 in PDF included into driver download):
Property names and values are case-sensitive.

JAVA: JDBC Connection by URL Aurora(mysql) aws

i want to connect to a D by an URL i got to make the conections.
static Connection cnx = null;
public static Connection obtener() throws SQLException{
if (cnx == null) {
try {
cnx = DriverManager.getConnection("jdbc:mysql://combis.cpgtn4fi7t5t.us-east-1.rds.amazonaws.com/am_myDataBase", "user", "pass");
}catch (SQLException es) {
// TODO: handle exception
throw new SQLException(es);
}
}
return cnx;
}
and i can not get the connection. Is it well written the URL?
got this Error:
java.sql.SQLException: java.sql.SQLException: No suitable driver found for jdbc:mysql://combis.cpgtn4fi7t5t.us-east-1.rds.amazonaws.com/am_myDataBase
I think, you need to do two things here that are:-
1.Load the mysql driver class.
Class.forName("com.mysql.jdbc.Driver");
2.Change the connection url like this-
jdbc:mysql://combis.cpgtn4fi7t5t.us-east-1.rds.amazonaws.com/am_myDataBase?useSSL=false
I hope it will work.
Use below to load driver before getting connection
Class.forName("<driver class name>");

Connection to Postgres Database failes

I'm trying for the first time to JDBC to connect to a local database (Postgres) but I can't seem to actually made the connection. This is my code:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class JDBCExample {
public static void main(String[] argv) {
System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/training", "iam47662285",
"MY56KZDZ");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
And this is the error that appears when i try to execute it:
-------- PostgreSQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
Connection Failed! Check output console
Nov 02, 2017 4:36:52 PM org.postgresql.Driver connect
SEVERE: Connection error:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "iam47662285"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:438)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
at org.postgresql.Driver.makeConnection(Driver.java:450)
at org.postgresql.Driver.connect(Driver.java:252)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at UF2.Pruebas.BDR_BDOO.JDBCExample.main(JDBCExample.java:33)
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "iam47662285"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:438)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
at org.postgresql.Driver.makeConnection(Driver.java:450)
at org.postgresql.Driver.connect(Driver.java:252)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at UF2.Pruebas.BDR_BDOO.JDBCExample.main(JDBCExample.java:33)
I use Fedora, but i don't know if that's relevant. Anyone have any idea of why is this happening?
Your username/password is incorrect in the following code:
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/training", "iam47662285",
"MY56KZDZ");
Manually log in to psql and verify what the correct username/password is. If necessary ALTER USER "xxxx" WITH PASSWORD ''; to a new one that you'll remember.
Also hardcoding login details is a very bad idea.

PostgreSQL JDBC Driver not working for Heroku DB Connection

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?
Use postgresql in your JDBC URL and not postgres.
Also, you need to change your DB password (because you posted it publicly) by running this command:
$ heroku pg:credentials DATABASE --reset
You need to load the driver class first by the class loader:
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(...);
Also see: What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
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);
}

Could not load the driver java.sql.SQLException: No suitable driver found for jdbc

I have a problem with connecting my java and mysql database was working fine yesterday but now is not working here is my code.
public static void main(String[] args) {
try
{
Class.forName ("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println ("Could not load the driver");
}
String user, pass, host, database;
user = "Michael";
pass = "Blaine22";
host = "localhost";
database = "maintenance_work";
Connection conn = DriverManager.getConnection
("jdbc:mysql://"+host+":3306/"+database, user, pass);
conn.setAutoCommit(false);
//Menu code:
appSchoolMaintenance newWork = new appSchoolMaintenance();
newWork.statement1(); // opens the start method
}
Add MySQL JDBC driver (you can get it here: http://dev.mysql.com/downloads/connector/j/) to application's classpath and remove unnecessary piece of code:
try
{
Class.forName ("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println ("Could not load the driver");
}
You can try to connect directly with MySQL JDBC driver
com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
Connection conn = driver.
connect("jdbc:mysql://localhost/test?user=root&password=root", null);
if this code does compile you are missing MySQL JDBC driver on the classpath
Started to work again - tried all the suggestions but started working again no rhyme or reason to it! thanks for the help though

Categories