rspec/rules/S4925/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

74 lines
2.0 KiB
Plaintext

== Why is this an issue?
In the past, it was required to load a JDBC driver before creating a ``++java.sql.Connection++``. Nowadays, when using JDBC 4.0 drivers, this is no longer required and ``++Class.forName()++`` can be safely removed because JDBC 4.0 (JDK 6) drivers available in the classpath are automatically loaded.
This rule raises an issue when ``++Class.forName()++`` is used with one of the following values:
* ``++com.mysql.jdbc.Driver++``
* ``++oracle.jdbc.driver.OracleDriver++``
* ``++com.ibm.db2.jdbc.app.DB2Driver++``
* ``++com.ibm.db2.jdbc.net.DB2Driver++``
* ``++com.sybase.jdbc.SybDriver++``
* ``++com.sybase.jdbc2.jdbc.SybDriver++``
* ``++com.teradata.jdbc.TeraDriver++``
* ``++com.microsoft.sqlserver.jdbc.SQLServerDriver++``
* ``++org.postgresql.Driver++``
* ``++sun.jdbc.odbc.JdbcOdbcDriver++``
* ``++org.hsqldb.jdbc.JDBCDriver++``
* ``++org.h2.Driver++``
* ``++org.firebirdsql.jdbc.FBDriver++``
* ``++net.sourceforge.jtds.jdbc.Driver++``
* ``++com.ibm.db2.jcc.DB2Driver++``
=== Noncompliant code example
[source,java]
----
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Demo {
private static final String DRIVER_CLASS_NAME = "org.postgresql.Driver";
private final Connection connection;
public Demo(String serverURI) throws SQLException, ClassNotFoundException {
Class.forName(DRIVER_CLASS_NAME); // Noncompliant; no longer required to load the JDBC Driver using Class.forName()
connection = DriverManager.getConnection(serverURI);
}
}
----
=== Compliant solution
[source,java]
----
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Demo {
private final Connection connection;
public Demo(String serverURI) throws SQLException {
connection = DriverManager.getConnection(serverURI);
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this "Class.forName()", it is useless.
endif::env-github,rspecator-view[]