使用Java+SQL Server开发CRUD Web应用时GlassFish部署失败求助
Hey there, let’s dig into why your Java CRUD app won’t deploy to GlassFish— that "Connection refused: connect" error is a classic clue pointing to database connectivity issues. Here’s a step-by-step breakdown to fix this:
1. Confirm SQL Server is Up and Reachable
- First, head to your Windows Services (type
services.mscin the Run dialog) and check if the SQL Server service is marked as Running. If it’s stopped, start it up. - Try connecting to your SQL Server instance using SQL Server Management Studio (SSMS). If SSMS can’t connect either, the problem is with the database itself, not your app.
2. Double-Check Your Database Connection String
Make sure your connection string (in persistence.xml, db.properties, or wherever you’ve defined it) is correctly formatted for SQL Server. A typical example looks like this:
jdbc:sqlserver://localhost:1433;databaseName=YourCRUDNewsDB;integratedSecurity=true;
- If you’re using SQL Server Authentication instead of Windows Auth, add
user=YourUsername;password=YourPassword;to the string. - Verify the port number: 1433 is the default, but if you changed it during SQL Server setup, use that custom port instead.
3. Validate GlassFish JDBC Configuration
- JDBC Driver: Ensure you’ve added the SQL Server JDBC driver (like
mssql-jdbc.jar) to GlassFish’sdomain/libfolder. Restart GlassFish after adding it— the server won’t pick up new drivers on the fly. - Connection Pool & Resource: If you set up a JDBC connection pool in GlassFish, double-check:
- The pool’s JDBC URL matches what you’re using in your app.
- The driver class is set to
com.microsoft.sqlserver.jdbc.SQLServerDriver. - Username/password (if using SQL Auth) are correctly entered.
4. Rule Out Firewall/Network Blockages
- Your local firewall might be blocking GlassFish from accessing SQL Server’s port. Try temporarily disabling the firewall to test— if the app deploys, you’ll need to add an exception for GlassFish or port 1433.
- If you’re connecting to a remote SQL Server, confirm:
- The server’s firewall allows incoming connections on your SQL port.
- SQL Server is configured to allow remote connections (enable TCP/IP in SQL Server Configuration Manager).
5. Check Full GlassFish Logs for More Clues
The error snippet you shared is cut off— GlassFish’s full logs will have the complete exception stack trace, which can reveal specific issues like missing drivers, invalid credentials, or wrong database names. You’ll find the logs at:
C:\Program Files\glassfish\domains\domain1\logs\server.log
Look for entries around the time you tried to deploy— they’ll point you straight to the root cause.
内容的提问来源于stack exchange,提问作者user8705895

