Answer the question
In order to leave comments, you need to log in
How to connect android studio to PostgreSQL?
At connections to a DB displays an error.
Program code
import android.os.StrictMode
import android.util.Log
import java.sql.*
import java.util.*
class dbConnect{
private val ip = "localhost:5432"
private val db = "postgres"
private val username = "postgres"
private val password = "1234"
fun databaseConnection(): Connection? {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
var conn: Connection? = null
var connString: String? = null
try{
Class.forName("org.postgresql.Driver")
connString = "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=1234"
conn = DriverManager.getConnection(connString)
}catch (e: SQLException){
println(connString)
e.printStackTrace()
}catch (ex: ClassNotFoundException){
println("Where is SQL Driver ")
ex.printStackTrace()
}catch (e: Exception){
Log.e("Error exeption", e.message.toString())
}
return conn
}
W/System.err: org.postgresql.util.PSQLException: The connection attempt failed.
W/System.err: at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
at org.postgresql.Driver.makeConnection(Driver.java:465)
W/System.err: at org.postgresql.Driver.connect(Driver.java:264)
at java.sql.DriverManager.getConnection(DriverManager.java:569)
at java.sql.DriverManager.getConnection(DriverManager.java:237)
at com.lebonid.myapp.DataBase.dbConnect.databaseConnection(dbConnect.kt:34)
at com.lebonid.myapp.MainActivity.onCreate(MainActivity.kt:12)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
at java.net.Socket.createImpl(Socket.java:487)
at java.net.Socket.connect(Socket.java:614)
at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
W/System.err: at org.postgresql.core.PGStream.<init>(PGStream.java:95)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
... 20 more
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question