如何解决为什么无法连接到数据库?
我连接到 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 举报,一经查实,本站将立刻删除。