Postgresql不连接到android,使用JDBC抛出org.postgresql.util。PSQLException:连接尝试失败。

时间:2023-01-10 23:06:39

I am developing an android application for which I need to connect to Postgresql database, I installed it 9.3 version, and checked in PGAdminIII it is connecting. I also created a java project in eclipse just for testing JDBC it is connecting successfully but when I am trying to connet to Postgresql from Android project it is throwing error: org.postgresql.util.PSQLException: The connection attempt failed. Here is the code Which I written in MainActivity.java

我正在开发一个android应用程序,我需要连接到Postgresql数据库,我安装了9.3版本,并在pgmanagii中进行了连接。我还在eclipse中创建了一个java项目,只是为了测试JDBC连接成功,但当我试图从Android项目中向Postgresql发送信息时,它抛出了错误:org.postgresql.util。PSQLException:连接尝试失败。这是我在MainActivity.java编写的代码。

@Override
protected Void doInBackground(Void... params) {
    try {

          Class.forName("org.postgresql.Driver");
          Connection  conn = DriverManager.getConnection("jdbc:postgresql://192.168.43.207:5432/testdb", "postgres", "password");
          System.out.println("connection success");
          conn.close() ;
        } 
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

and pg_hba.conf

和pg_hba.conf

IPv4 local connections:

host all all 192.168.43.207 trust

主机所有192.168.43.207信任。

IPv6 local connections:

host all all ::1/128 trust

主机所有::1/128信任。

postgresql.conf

postgresql.conf

listen_addresses = '*'

listen_addresses = ' * '

Note: If I am changing ip address 192.168.43.207 with localhost/127.0.0.1 it is throwing error- org.postgresql.util.psqlexception connection refused. check that the hostname and port are correct I have googled about it and also seen posts on * but nothing works. I am using windows7 please provide solution according to it. please help me as I am fighting it for last two days but nothing works.

注意:如果我用localhost/127.0.0.1更改ip地址192.168.43.207,那就是抛出错误。psqlexception连接拒绝了。检查主机名和端口是否正确,我在google上搜索过它,也看到了*上的帖子,但是没有任何工作。我使用的是windows7,请根据它提供解决方案。请帮助我,因为我在过去的两天与它斗争,但没有任何工作。

Edit: when I run these commands from CMD in windows I got following outout

编辑:当我在windows的CMD中运行这些命令时,我得到了输出。

C:\Windows\system32>sc query postgresql-9.3

SERVICE_NAME: postgresql-9.3
    TYPE               : 10  WIN32_OWN_PROCESS
    STATE              : 4  RUNNING
                            (STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
    WIN32_EXIT_CODE    : 0  (0x0)
    SERVICE_EXIT_CODE  : 0  (0x0)
    CHECKPOINT         : 0x0
    WAIT_HINT          : 0x0

C:\Windows\system32>netstat -a | findstr 5432
  TCP    0.0.0.0:5432           -PC:0           LISTENING
  TCP    [::]:5432              -PC:0           LISTENING
  TCP    [::1]:5432             -PC:49573       ESTABLISHED
  TCP    [::1]:5432             -PC:49574       ESTABLISHED

1 个解决方案

#1


2  

Your problem is that 127.0.0.1 on the Android goes to the virtual Android device and not to your machine hosting the PostgreSQL database. Referencing the Android emulator documentation for networking:

你的问题是,在Android上的127.0.0.1是虚拟的Android设备,而不是你的机器托管PostgreSQL数据库。参考Android模拟器文档的联网:

Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.

仿真器的每个实例都运行在虚拟路由器/防火墙服务的后面,该服务将它与您的开发机器的网络接口和设置和internet隔离开来。模拟设备无法在网络上看到您的开发机器或其他仿真器实例。相反,它只看到它通过以太网连接到路由器/防火墙。

And below that, there is a table for the virtual router:

下面是虚拟路由器的表格:

Postgresql不连接到android,使用JDBC抛出org.postgresql.util。PSQLException:连接尝试失败。

And further down in a section about using 127.0.0.1 (emphasis mine):

在一个关于使用127.0.0.1(强调我的)的章节中:

Also note that the address 127.0.0.1 on your development machine corresponds to the emulator's own loopback interface. If you want to access services running on your development machine's loopback interface (a.k.a. 127.0.0.1 on your machine), you should use the special address 10.0.2.2 instead.

还要注意,在您的开发机器上的地址127.0.0.1对应于模拟器自己的环回接口。如果您想要在您的开发机器的loopback接口上运行服务(也就是您的机器上的127.0.0.1),您应该使用特殊的地址10.0.2.2。

You should use 10.0.2.2 rather than 127.0.0.1 if you want to connect to the development machine.

如果您想要连接到开发机器,应该使用10.0.2.2而不是127.0.0.1。

#1


2  

Your problem is that 127.0.0.1 on the Android goes to the virtual Android device and not to your machine hosting the PostgreSQL database. Referencing the Android emulator documentation for networking:

你的问题是,在Android上的127.0.0.1是虚拟的Android设备,而不是你的机器托管PostgreSQL数据库。参考Android模拟器文档的联网:

Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.

仿真器的每个实例都运行在虚拟路由器/防火墙服务的后面,该服务将它与您的开发机器的网络接口和设置和internet隔离开来。模拟设备无法在网络上看到您的开发机器或其他仿真器实例。相反,它只看到它通过以太网连接到路由器/防火墙。

And below that, there is a table for the virtual router:

下面是虚拟路由器的表格:

Postgresql不连接到android,使用JDBC抛出org.postgresql.util。PSQLException:连接尝试失败。

And further down in a section about using 127.0.0.1 (emphasis mine):

在一个关于使用127.0.0.1(强调我的)的章节中:

Also note that the address 127.0.0.1 on your development machine corresponds to the emulator's own loopback interface. If you want to access services running on your development machine's loopback interface (a.k.a. 127.0.0.1 on your machine), you should use the special address 10.0.2.2 instead.

还要注意,在您的开发机器上的地址127.0.0.1对应于模拟器自己的环回接口。如果您想要在您的开发机器的loopback接口上运行服务(也就是您的机器上的127.0.0.1),您应该使用特殊的地址10.0.2.2。

You should use 10.0.2.2 rather than 127.0.0.1 if you want to connect to the development machine.

如果您想要连接到开发机器,应该使用10.0.2.2而不是127.0.0.1。