PostgreSQL里没有连接使用JDBC投掷org.postgresql.util.PSQLException到Android:连接尝试失败。 [英] Postgresql not connecting to android using JDBC throwing org.postgresql.util.PSQLException: The connection attempt failed

查看:5219
本文介绍了PostgreSQL里没有连接使用JDBC投掷org.postgresql.util.PSQLException到Android:连接尝试失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,我需要连接到PostgreSQL数据库,我安装了它9.3版本,并检查了PGAdminIII它连接。我也是在Eclipse中创建一个Java项目只是用于测试JDBC它成功连接,但是当我试图从Connet的Andr​​oid项目到PostgreSQL它抛出错误:org.postgresql.util.PSQLException:连接尝试失败。  这是我写在MainActivity.java的code

  @覆盖
保护无效doInBackground(虚空...... PARAMS){
    尝试 {

          的Class.forName(org.postgresql.Driver);
          数Connection conn = DriverManager.getConnection就(JDBC:PostgreSQL的://192.168.43.207:5432 / TESTDB,Po​​stgres的,密码);
          的System.out.println(连接成功);
          conn.close();
        }
        赶上(的SQLException E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
    赶上(例外五){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    返回null;
 

的pg_hba.conf

的IPv4本地连接:

主机的所有一切192.168.43.207信任

IPv6本地连接:

主机的所有一切:: 1/128信任

的postgresql.conf

的listen_addresses ='*'

请注意:如果我改变IP地址192.168.43.207,将localhost / 127.0.0.1它扔无差错     org.postgresql.util.psqlexception连接被拒绝。检查主机名和端口是否正确     我用Google搜索一下,也看到帖子上的#1,但没有任何工程。 我使用的的windows7 请根据它提供的解决方案。 请帮助我,我战斗它过去两天,但没有任何工程。

编辑:当我在Windows中运行这些命令,从CMD我得到了以下outout

  C:\ WINDOWS \ SYSTEM32> SC查询与PostgreSQL 9.3

SERVICE_NAME:与PostgreSQL 9.3
    TYPE:10 WIN32_OWN_PROCESS
    STATE:4 RUNNING
                            (可停,可暂停,ACCEPTS_SHUTDOWN)
    WIN32_EXIT_ code:0(为0x0)
    SERVICE_EXIT_ code:0(为0x0)
    检查点:为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
 

解决方案

您的问题是, 127.0.0.1 在Android进入到虚拟的Andr​​oid设备,而不是你的机托管PostgreSQL数据库。引用href="http://developer.android.com/tool​​s/devices/emulator.html#emulatornetworking" rel="nofollow"> Android模拟器,文档,网络的

  

模拟器的每个实例运行一个虚拟路由器/防火墙后面   服务,从您的开发机器的网络隔离开来   接口和设置,并从互联网上。一个模拟装置可   看不到自己的开发机器或其他模拟器实例上   网络。相反,它只能看到,它是通过以太网连接   到一个路由器/防火墙

和下面的是,有一个表为虚拟路由器:

和在一节进一步下跌关于使用 127.0.0.1 (重点煤矿):

  

另外请注意,在开发计算机的地址127.0.0.1   对应于模拟器自身的loopback接口。如果你想   在开发机器上的回环运行接入服务   接口(又名127.0.0.1你的机器上),您应该使用   特殊地址10.0.2.2代替。

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

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

@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

IPv4 local connections:

host all all 192.168.43.207 trust

IPv6 local connections:

host all all ::1/128 trust

postgresql.conf

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 Stackoverflow 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.

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

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

解决方案

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:

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.

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

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

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.

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

这篇关于PostgreSQL里没有连接使用JDBC投掷org.postgresql.util.PSQLException到Android:连接尝试失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆