58 lines
1.8 KiB
Plaintext
58 lines
1.8 KiB
Plaintext
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
|
|
|
|
----
|
|
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
|
|
|
|
----
|
|
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);
|
|
}
|
|
}
|
|
----
|
|
|