-1

https://mariadb.com/kb/en/about-mariadb-connector-j/ https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html

In that site, Class.forName is no longer needed. (that file based Java11, JavaEE8 with gradle)

but In my case.

If I didn't use this.

Class.forName("org.mariadb.jdbc.Driver")


HTTP:500 error occurred and that error message is

java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydb

Otherwise when I use that code, It work well.

Class.forName("org.mariadb.jdbc.Driver")

Why do they need [Class.forName]?

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%
    Connection conn = null;

    Class.forName("org.mariadb.jdbc.Driver");

    try {
        conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/mydb", "root", "1234"); 
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

    conn.setAutoCommit(false);
%>
Moon
  • 11
  • 2

1 Answers1

0

The JDBC 4 standard introduced automatic registration of JDBC drivers. Earlier drivers had to manually be registered by forcing the classloader to load them, e.g., by calling Class.forName.

It seems you're using an older version of the driver that still requires the Class.forName call.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • In gradle.build that implements 'org.mariadb.jdbc:mariadb-java-client:3.0.7' MariaDB Connector/J 3.0's JDBC version is 4.2. I don't understand what happened : – Moon Dec 15 '22 at 18:20