为什么无法连接到数据库?

问题描述

我连接到 postgres 数据库。但是每次都显示无法连接数据库错误E/sql 错误:连接尝试失败。 同时,它通常通过android studio中的插件连接。可能是什么问题呢? 请详细描述可能是什么问题及其解决方案。

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){
            Log.e("sql Error",e.message.toString())
            println(connString)
        }catch (ex: ClassNotFoundException){
            Log.e("True ClassNot",ex.message.toString())
        }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)
W/System.err:     at org.postgresql.Driver.makeConnection(Driver.java:465)
        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:29)
W/System.err:     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)
        at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
W/System.err:     at android.app.ActivityThread.-wrap11(UnkNown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
W/System.err: Caused by: java.net.socketException: socket Failed: EACCES (Permission denied)
        at java.net.socket.createImpl(Socket.java:487)
W/System.err:     at java.net.socket.connect(Socket.java:614)
        at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
        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)
W/System.err:   ... 20 more

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)