简体   繁体   English

android postgresql数据库连接错误

[英]android postgresql database connection error

i try to connect my android app with my postgresql Databse but it doesn't get a connection the dr iver is running but the connection is not working. 我尝试将我的android应用程序与我的postgresql Databse连接,但是dr iver正在运行却没有连接,但该连接无法正常工作。

Here is the java code: 这是Java代码:

private void initDatabase() {

    try {

        Class.forName("org.postgresql.Driver");

    } catch (ClassNotFoundException e) {

        return;

    }

    Connection connection = null;
    String url = "jdbc:postgresql://10.0.2.2/Seminarkurs";

    try {

        connection = DriverManager.getConnection(url , "postgres", "postgres");
        Toast.makeText(getApplicationContext(), "Connection successful!", Toast.LENGTH_LONG).show();

        connection.close();

    } catch (SQLException e) {

        Toast.makeText(getApplicationContext(), "Connection Failed! Check output console", Toast.LENGTH_LONG).show();
        e.printStackTrace();
        return;

    }

}

and here is the logcat: 这是logcat:

01-16 14:40:17.192    8044-8053/com.example.damian.schulkalender E/System﹕ Uncaught exception thrown by finalizer
01-16 14:40:17.192    8044-8053/com.example.damian.schulkalender E/System﹕ java.lang.NullPointerException
            at org.postgresql.jdbc2.AbstractJdbc2Connection.close(AbstractJdbc2Connection.java:677)
            at org.postgresql.jdbc2.AbstractJdbc2Connection.finalize(AbstractJdbc2Connection.java:1036)
            at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:187)
            at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
            at java.lang.Thread.run(Thread.java:841)

I'm sure that this isn't what you want to hear, but it's really not a reasonable thing to do to try to connect directly to a server-side database from a mobile device. 我确定这不是您想听到的,但是尝试从移动设备直接连接到服务器端数据库确实不是一件合理的事。 I see this question here several times a week, and I'm really not sure why. 我每周在这里看到几次这个问题,我真的不确定为什么。

The preferred way to do this is to create a server-side application that provides data to your mobile app via an HTTP connection (eg a REST Api). 执行此操作的首选方法是创建一个服务器端应用程序,该应用程序通过HTTP连接(例如REST Api)为您的移动应用程序提供数据。

That said, it looks like your attempt to create the connection is failing. 也就是说,您创建连接的尝试似乎失败。 You may get more info by replacing your e.printStackTrace() in your exception handler to the use of the Android logger, so that you can see the error in logcat. 通过将异常处理程序中的e.printStackTrace()替换为Android记录器,可以获取更多信息,以便可以在logcat中看到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM