在Windows 10系统下使用Eclipse访问PostgreSQL时出错
Hey there! Let's work through this connection issue together. I'll walk you through the most common fixes based on your setup (Windows 10, JRE 10, Eclipse Oxygen 3a) and the code you shared.
First: Fix Your Incomplete Code
Your catch block has a truncated System... statement—this is preventing you from seeing the full error message, which is critical for debugging. Let's first update that to capture all details:
import java.sql.Connection; import java.sql.DriverManager; public class PostgreSQLJDBC { public static void main(String args[]) { Connection c = null; try { // Note: For JDBC 4.2+, Class.forName() is optional if the driver is properly registered Class.forName("org.postgresql.Driver"); c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "123"); System.out.println("Connection successful!"); } catch (Exception e) { // Print the full stack trace to see exactly what's failing e.printStackTrace(); // Add a clear error message for quick reference System.err.println("\nError details: " + e.getMessage()); } finally { // Always close connections to avoid resource leaks try { if (c != null) c.close(); } catch (Exception e) { e.printStackTrace(); } } } }
Run this updated code first—this will give you a specific error message (like "connection refused", "invalid password", or "database not found") that we can use to narrow down the issue.
Common Fixes Based on Your Setup
1. Verify PostgreSQL Driver Compatibility
JRE 10 requires a PostgreSQL JDBC driver version that supports Java 10. Use PostgreSQL JDBC Driver 42.2.5 or newer (older versions may have compatibility issues). Here's how to add it in Eclipse:
- Download the driver JAR from the PostgreSQL official site
- Create a
libfolder in your Eclipse project, drop the JAR into it - Right-click the JAR → Build Path → Add to Build Path
2. Check if PostgreSQL Service is Running
On Windows 10:
- Press
Win + R, typeservices.mscand hit Enter - Look for a service named like
postgresql-x64-XX(XX is your PostgreSQL version) - Make sure its status is Running. If not, right-click it → Start
3. Validate Connection Parameters
Double-check these details:
- Database name: Does
testdbactually exist in your PostgreSQL instance? Open pgAdmin and confirm it's listed under your server. - Credentials: Can you log into PostgreSQL using
postgresand password123via pgAdmin? If not, reset the password or use the correct credentials. - Port: Is PostgreSQL using the default port
5432? If you changed it during installation, update the connection URL to match (e.g.,jdbc:postgresql://localhost:5433/testdb).
4. Check Windows Firewall Settings
Windows Firewall might be blocking the connection to port 5432:
- Temporarily disable the firewall (for testing only) and run your code again. If it works, add an exception:
- Go to Windows Defender Firewall → Advanced Settings → Inbound Rules
- Create a new rule to allow traffic on port 5432, or allow your Eclipse/Java executable to communicate through the firewall.
5. Optional: Use Try-With-Resources (Better Practice)
For cleaner code and automatic resource management, use Java's try-with-resources syntax (works with JRE 10):
import java.sql.Connection; import java.sql.DriverManager; public class PostgreSQLJDBC { public static void main(String args[]) { try (Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres", "123")) { System.out.println("Successfully connected to PostgreSQL!"); } catch (Exception e) { e.printStackTrace(); System.err.println("\nError details: " + e.getMessage()); } } }
Once you run the updated code and get the specific error message, you can narrow down the fix even further. Feel free to share the error details if you need more targeted help!
内容的提问来源于stack exchange,提问作者Tipo33

