当前位置:网站首页>在JDBC连接数据库时报错:Connection to 139.9.130.37:15400 refused.
在JDBC连接数据库时报错:Connection to 139.9.130.37:15400 refused.
2022-06-13 03:28:00 【CSDN问答】
问题遇到的现象和发生背景
今天想再次用teacher给的java代码,通过JDBC连接数据库执行对数据库内数据的操作(之前有成功过,这次再试一次就不行了),前面的配置都做好了,就是到了命令台运行的时候发生了错误,错误显示如下:(说明一下,我用的是华为云弹性服务器连接本地虚拟机上的数据库)
问题相关代码,请勿粘贴截图
java代码如下:
import java.sql.*;
public class openGaussDemo {
static final String JDBC_DRIVER = "org.postgresql.Driver"; static final String DB_URL = "jdbc:postgresql://139.9.130.37:15400/demo"; // 数据库的用户名与密码,需要根据自己的设置static final String USER = "dbuser";static final String PASS = "Gauss#3demo"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); // 执行查询 System.out.println(" 实例化Statement对象..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, name, url FROM demo.websites"; ResultSet rs = stmt.executeQuery(sql); // 展开结果集数据库 while(rs.next()){ // 通过字段检索 int id = rs.getInt("id"); String name = rs.getString("name"); String url = rs.getString("url"); // 输出数据 System.out.print("ID: " + id); System.out.print(", 站点名称: " + name); System.out.print(", 站点 URL: " + url); System.out.print("\n"); } // 完成后关闭 rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ // 处理 JDBC 错误 se.printStackTrace(); }catch(Exception e){ // 处理 Class.forName 错误 e.printStackTrace(); }finally{ // 关闭资源 try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// 什么都不做 try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!");}
}
(以上都是复制粘贴修改了一些根据华为云服务器的参数)
运行结果及报错内容
在命令台通过javac -encoding utf-8 -cp d:\Download\postgresql.jar openGaussDemo.java
和 java -cp .;D:/Download/postgresql.jar openGaussDemo命令后报错:
大致意思就是要不就是hostname错误要不就是端口号错误,但是已经反复检查都是没有错的,然后就有可能是后面的postmaster不接受TCP/IP的连接,但是都是按teacher给的文档来做的,而且之前都有运行成功过,就不知道到底是哪里出了问题。
我的解答思路和尝试过的方法
我上网搜了好多相关的信息,试过关防火墙,也试过重启虚拟数据库,配置了一些参数,看了一下监听地址是*没错,也将远程连接的权限赋给了我的dbuser用户,统统都失败了,还有的说要在win+R之后输入services.msc开启postgresql服务,看了下发现自己的电脑里面也没有这个服务。
我想要达到的结果
我就是想跑通这个java程序,ball ball各位help help小的吧!要做不完了!
边栏推荐
- On the career crisis of programmers at the age of 35
- MySQL learning summary 9: non empty constraints, uniqueness constraints, primary key, auto_ Increment, foreign key, default, etc
- Pollution discharge fees of listed companies 2010-2020 & environmental disclosure level of heavy pollution industry - original data and calculation results
- Use of compact, extract and list functions in PHP
- Masa auth - overall design from the user's perspective
- 2-year experience summary to tell you how to do a good job in project management
- Differences between XAML and XML
- Personal understanding of grammar sugar
- How to Draw Useful Technical Architecture Diagrams
- P1048 [noip2005 popularization group] Drug collection
猜你喜欢
Video playback has repeatedly broken 1000w+, how to use the second dimension to create a popular model in Kwai
Peking University HP financial index - matching enterprise green innovation index 2011-2020: enterprise name, year, industry classification and other multi indicator data
Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
Masa auth - SSO and identity design
[azure data platform] ETL tool (1) -- Introduction to azure data factory
Typical application of ACL
技术博客,经验分享宝典
C simple understanding - generics
[azure data platform] ETL tool (5) -- use azure data factory data stream to convert data
[JVM Series 2] runtime data area
随机推荐
Transaction processing in PDO
Prefecture level city - air flow coefficient data - updated to 2019 (including 10m wind speed, boundary height, etc.)
Stack information, GC statistics
Four ways of array traversal in PHP
Array in PHP array function_ Slice and array_ flip
[JVM Series 7] garbage collector
Common command records of redis client
Spark Foundation
MySQL learning summary 6: data type, integer, floating point number, fixed-point number, text string, binary string
(九)详解广播机制
Use of jstack
MySQL learning summary Xi: detailed explanation of the use of stored procedures and stored functions
Azure SQL db/dw series (13) -- using query store (2) -- report Introduction (2)
Configuration and practice of shardingsphere JDBC sub database separation of read and write
Economic panel topic 1: panel data of all districts and counties in China - more than 70 indicators such as population, pollution and agriculture (2000-2019)
C simple understanding - overloaded operator
2000-2019 enterprise registration data of provinces, cities and counties in China (including longitude and latitude, number of registrations and other multi indicator information)
MySQL learning summary 9: non empty constraints, uniqueness constraints, primary key, auto_ Increment, foreign key, default, etc
look on? What is the case between neo4j and ongdb?
Understanding the ongdb open source map data foundation from the development of MariaDB