I'm doing a project at school where I need to connect a website and my android app to a PostgreSQL database, I have tried to connect directly using JDBC, but it didn't work at all. I also have read about using some API, but I didn't understand how to do it I'm clueless about what to do now Does anyone know how to connect an android app to PostgreSQL? I will be eternally grateful I really need to do finish this project.
Edit : The problem with jdbc is that console always printed "Connection failed" Here is my JDBC attempt
import java.sql.DriverManager
class Database {
private var connection: Connection? = null
private val host = "i replaced this so i dont lose controll over the database*eu-west-1.compute.amazonaws.com" // For Google Cloud Postgresql
private val database = "*****mcl8"
private val port = ****
private val user = "****wwzntag"
private val pass = "*****2c82a7dd7"
private var url = "jdbc:postgresql://xhlgovzwwzntag:ccf199dc***********cb6a1865e87e7532da842c82a7dd7@ec2-34-255-134-200.eu-west-1.compute.amazonaws.com:5432/d2rlvf0bcqmcl8"
private var status = false
private fun connect() {
val thread = Thread {
try {
Class.forName("org.postgresql.Driver")
connection = DriverManager.getConnection(url, user, pass)
status = true
println("connected:$status")
} catch (e: Exception) {
status = false
print(e.message)
e.printStackTrace()
}
}
thread.start()
try {
thread.join()
} catch (e: Exception) {
e.printStackTrace()
status = false
}
}
val extraConnection: Connection?
get() {
var c: Connection? = null
try {
Class.forName("org.postgresql.Driver")
c = DriverManager.getConnection(url, user, pass)
} catch (e: Exception) {
e.printStackTrace()
}
return c
}
init {
url = String.format(url, host, port, database)
connect()
//this.disconnect();
println("connection status:$status")
}
}```