You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetBeans8+JDK8+GlassFish环境下Servlet连接MySQL出现类未找到异常

Fixing ClassNotFoundException for com.mysql.jdbc.Driver in NetBeans 8 + GlassFish + JDK 8

Hey there, let's tackle this ClassNotFoundException issue you're facing with your Servlet-MySQL connection. I've seen this problem countless times, and it almost always comes down to the MySQL driver jar not being properly accessible to the GlassFish server at runtime—even if you added it to your project's library in NetBeans. Here's how to fix it step by step:

1. Ensure the MySQL Driver Jar is Deployed to the Server's Classpath

Adding the jar to your NetBeans project library only makes it available during development; GlassFish needs access to it when running your Servlet. You have two reliable options:

Option A: Deploy Globally to GlassFish

  • Locate your GlassFish installation directory, navigate to domains/domain1/lib (assuming you're using the default domain).
  • Copy your mysql-connector-java-*.jar file into this lib folder.
  • Restart GlassFish—this ensures the server picks up the new jar.

Option B: Package the Jar with Your Web Application

This is better if you want the driver to be part of your project's WAR file:

  • In your NetBeans project, right-click the WEB-INF folder → New → Folder, name it lib.
  • Copy your MySQL driver jar into this newly created WEB-INF/lib folder.
  • Right-click the jar file in NetBeans → Select "Add as Library" to ensure it's linked properly.
  • When you build and deploy your project, the jar will be included in the WAR, so GlassFish can find it.

2. Verify the Driver Class Name Matches Your MySQL Version

Double-check that you're using the correct driver class for your MySQL version:

  • For MySQL 5.x: Use com.mysql.jdbc.Driver (this is what you have in your code, which is fine if you're on MySQL 5).
  • For MySQL 8.0+: The driver class was changed to com.mysql.cj.jdbc.Driver. If you're using MySQL 8+, update this line in your code, and also add timezone parameters to your JDBC URL like this:
    String url = "jdbc:mysql://localhost:3306/your_db_name?useSSL=false&serverTimezone=UTC";
    

3. Example Corrected Connection Code

Here's a complete, working Servlet snippet to test your connection:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DBConnectServlet extends HttpServlet {

    // Update these values with your database details
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String DB_USER = "your_username";
    private static final String DB_PASS = "your_password";
    // Adjust driver class based on MySQL version
    private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; // MySQL 5.x
    // private static final String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver"; // MySQL 8+

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            Connection conn = null;
            try {
                // Load driver (JDK 6+ can auto-load via ServiceLoader, but explicit call works too)
                Class.forName(DRIVER_CLASS);
                conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
                out.println("<h3>✅ Database connection successful!</h3>");
            } catch (ClassNotFoundException e) {
                out.println("<h3>❌ Driver class not found: " + e.getMessage() + "</h3>");
                e.printStackTrace();
            } catch (SQLException e) {
                out.println("<h3>❌ Connection failed: " + e.getMessage() + "</h3>");
                e.printStackTrace();
            } finally {
                // Clean up connection
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

Final Notes

  • After making these changes, clean and rebuild your NetBeans project before redeploying to GlassFish.
  • If you still run into issues, check that the driver jar version is compatible with your JDK 8 (use mysql-connector-java 5.1.49 or later for MySQL 5.x, or 8.0.28+ for MySQL 8.x—both work with JDK 8).

内容的提问来源于stack exchange,提问作者ronak roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:42:43