Issue
I'm trying to connect to postgresql from Heroku (DB is by amazon aws) in Android java
In my host url address is like this
postgres://username:password@host:port/database
but with jdbc connect I'm using
jdbc:postgresql://username:password@host:port/database
I'm not sure if that's the problem because I couldn't find any gradle dependency for that
This is my build.gradle
implementation group: 'org.postgresql', name: 'postgresql', version: '42.4.0'
This is my Java code
private String url = "jdbc:postgresql://username:password@host:port/database";
private Connection connection;
private void connect() {
try {
DriverManager.registerDriver(new org.postgresql.Driver());
connection = DriverManager.getConnection(url);
} catch (Exception e) {
System.out.print(e.getMessage());
e.printStackTrace();
}
}
and when I run the program I get this error
W/System.err: org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:331)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
at org.postgresql.Driver.makeConnection(Driver.java:402)
at org.postgresql.Driver.connect(Driver.java:261)
W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:580)
at java.sql.DriverManager.getConnection(DriverManager.java:236)
at com.example.myapplication.Database$1.run(Database.java:33)
at java.lang.Thread.run(Thread.java:929)
Caused by: java.net.UnknownHostException: username:password@host
W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:208)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:621)
at org.postgresql.core.PGStream.createSocket(PGStream.java:241)
at org.postgresql.core.PGStream.<init>(PGStream.java:98)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
... 8 more
Can anyone help me please to solve this?
Solution
I couldn't find any solution to that and since I'm developing Android applications, the best option I had was to switch to Firebase database, performance, security and everything is great on firebase, it's also free from Google and that's solved my problem.
Answered By - SpacemanSps
Answer Checked By - Mary Flores (JavaFixing Volunteer)