当前位置:网站首页>在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小的吧!要做不完了!
边栏推荐
- Array in PHP array function_ Slice and array_ flip
- Stack information, GC statistics
- Application framework / capability blueprint
- C method parameter: out
- [JVM series 4] common JVM commands
- DTCC | 2021 China map database technology conference link sharing
- [azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)
- Complex network analysis capability based on graph database
- Neil eifrem, CEO of neo4j, interprets the chart data platform and leads the development of database in the next decade
- C # simple understanding - method overloading and rewriting
猜你喜欢
Feign based remote service invocation
Azure SQL db/dw series (10) -- re understanding the query store (3) -- configuring the query store
Neo4j auradb free, the world's leading map database
Complex network analysis capability based on graph database
Alibaba cloud OSS access notes
ONNX+TensorRT+YoloV5:基于trt+onnx得yolov5部署1
Environmental pollution, enterprises, highways, fixed assets, foreign investment in all prefecture level cities in China - latest panel data
Carbon neutralization & Patent Innovation: multi indicator data such as patent panels (original documents) of provinces, cities and counties, and the number of low-carbon patents authorized
To resolve project conflicts, first-class project managers do so
通过Web ETL统一调度和管理DataX任务
随机推荐
Summary of rust language practice
Stack information, GC statistics
[azure data platform] ETL tool (2) -- azure data factory "copy data" tool (cloud copy)
Review notes of RS data communication foundation STP
Environmental pollution, enterprises, highways, fixed assets, foreign investment in all prefecture level cities in China - latest panel data
English语法_方式副词-位置
Simple use of qtreeview of QT (including source code + comments)
Carbon neutralization & Patent Innovation: multi indicator data such as patent panels (original documents) of provinces, cities and counties, and the number of low-carbon patents authorized
YoloV5-Face+TensorRT:基于WIN10+TensorRT8.2+VS2019得部署
Brew tool - "fatal: could not resolve head to a revision" error resolution
[azure data platform] ETL tool (1) -- Introduction to azure data factory
[JVM Series 5] performance testing tool
C method parameter: ref
The most complete ongdb and neo4j resource portal in history
Feign based remote service invocation
(九)详解广播机制
Figure data * reconstruction subgraph
MySQL learning summary 8: addition, deletion and modification of data processing
Panel for measuring innovation efficiency of 31 provinces in China (using Malmquist method)
Three ways to start WPF project