java – Postgresql没有使用JDBC抛出org.postgresql.util.PSQLException连接到android:连接尝试失败
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Postgresql没有使用JDBC抛出org.postgresql.util.PSQLException连接到android:连接尝试失败,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2717字,纯文字阅读大概需要4分钟。
内容图文
我正在开发一个Android应用程序,我需要连接到Postgresql数据库,我安装它9.3版本,并检查PGAdminIII它是连接.我还在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;
和pg_hba.conf
IPv4本地连接:
托管所有192.168.43.207信任
IPv6本地连接:
托管所有:: 1/128信任
postgresql.conf中
listen_addresses =’*’
注意:如果我用localhost / 127.0.0.1更改IP地址192.168.43.207,则会抛出错误 –
org.postgresql.util.psqlexception连接被拒绝.检查主机名和端口是否正确
我已经google了它,也看过Stackoverflow上的帖子,但没有任何作用.
我正在使用windows7请根据它提供解决方案.
请帮助我,因为我在最后两天打架但没有任何作用.
编辑:当我在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
解决方法:
您的问题是Android上的127.0.0.1进入虚拟Android设备而不是托管PostgreSQL数据库的计算机.参考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.
在此之下,有一个虚拟路由器表:
关于使用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.
如果要连接到开发计算机,则应使用10.0.2.2而不是127.0.0.1.
内容总结
以上是互联网集市为您收集整理的java – Postgresql没有使用JDBC抛出org.postgresql.util.PSQLException连接到android:连接尝试失败全部内容,希望文章能够帮你解决java – Postgresql没有使用JDBC抛出org.postgresql.util.PSQLException连接到android:连接尝试失败所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。