Java程序连接Oracle 10g时数据库端口号报错问题求助
Hey there! Let's work through this port-related error you're hitting when trying to connect your Java program to Oracle 10g. I've dealt with similar headaches before, so here are the key checks and fixes to try:
1. Verify the Oracle Listener is Running
First up, make sure Oracle's listener service is active and listening on port 1521. Here's how to check:
- Open a command prompt and run:
lsnrctl status - Look for output that mentions
PORT: 1521and your database SIDamaniin the "Services Summary" section. - If the listener isn't running, start it with:
lsnrctl start
2. Double-Check Your JDBC URL Format
Your current URL is jdbc:oracle:thin:@localhost:1521:amani—this uses the SID format, which is correct for Oracle 10g, but let's confirm a few details:
- Is
amanidefinitely your database's SID? You can verify this in thetnsnames.orafile (usually found inOracle_home\network\admin). Look for an entry like:AMANI = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SID_NAME = amani) ) ) - If your setup uses a service name instead of SID (common in some configurations), switch to the service name URL format:
jdbc:oracle:thin:@//localhost:1521/amani
3. Ensure Port 1521 Isn't Blocked or Occupied
- On Windows, run this command to check if another process is using port 1521:
netstat -ano | findstr :1521 - If you see an unexpected process ID (PID) listed, you can either terminate that process (if safe) or reconfigure Oracle to use a different port (not ideal, but an option if needed).
- Also, make sure your local firewall isn't blocking incoming/outgoing traffic on port 1521.
4. Confirm Your JDBC Jar Compatibility
- You mentioned adding the jar to Oracle's bin and JDK—make sure you're using the correct jar for Oracle 10g, typically
ojdbc14.jar(matches JDK 1.4/1.5, which is standard for 10g). Using a newer jar (like ojdbc6/7) might cause compatibility issues. - In JCreator, don't just copy the jar to JDK directories—manually add it to your project's classpath: Right-click your project → Properties → Libraries → Add External Jar, then select your ojdbc14.jar file.
5. Test the Connection with SQL*Plus First
Before debugging your Java code, confirm the connection works with SQL*Plus:
- Run this command in the prompt:
sqlplus system/a123@localhost:1521:amani - If SQL*Plus can't connect, the problem is with Oracle's configuration (listener, SID, credentials), not your Java code. Fix that first, then go back to your program.
If you try all these steps and still get errors, share the exact error message you're seeing—it'll help narrow things down even more!
内容的提问来源于stack exchange,提问作者Amani

