Maven adds oracle database jdbc dependency solution

 

Reprinted from: https://blog.csdn.net/erlian1992/article/details/74279106

 

Today, when I used IDEA to build a Maven project, I found that the Oracle database JDBC driver dependency could not be used, so I searched on the Internet. The following is the reason why it cannot be used.
Due to the Oracle authorization problem, Maven3 does not provide the oracle JDBC driver. We can also search for the ojdbc driver package in the Maven center, but we can see that the version is too old, and even if there are coordinates, it cannot be downloaded.

In order to use the Oracle JDBC driver in a project built with Maven, we must manually add Oracle's JDBC driver dependency to the local repository.
Since we want to manually add Maven dependencies to the local warehouse, we must first obtain the JDBC driver package of the Oracle database, which we can obtain in the following ways:
Method 1: Download the corresponding version from the Oracle official website
We enter in the browser:
http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html

Since the Oracle database version I installed is 11.2.0.1.0, I chose to download the corresponding 11g version
For version 11.2.0.1.0, we click on the connection selected in the above figure and find the corresponding 11.2.0.1.0 version module on the page

 
Since the JDK version used by the Oracle database JDBC driver package is different, we must download the corresponding JDK version of the Oracle database JDBC driver. The ojdbc.jar driver package is selected for JDK1.5, and the JDK1.6 and above versions are selected. ojdbc6.jar driver package, my JDK environment is JDK1.8, then select ojdbc6.jar driver package.
The above method may not be able to download for some reason.
Method 2: Obtain from the installation directory of the Oracle database (recommended)
After we install the Oracle database, we can find the JDBC driver package of the Oracle database in the corresponding database directory. This directory is not uniform. It is specified by ourselves during installation. My database instance is installed in this directory.

Then we find the database driver in this database instance, usually in the {oracle instance installation directory}/dbhome_1/jdbc/lib directory

Replenish
This directory contains 9 jar packages, ojdbc5.jar and ojdbc6.jar, which are two database-driven jar packages. The above has already been said very clearly, so I won't repeat them.
After obtaining the JDBC driver of the Oracle database, the next step is to manually add the JDBC driver dependency of the Oracle database to Maven's local warehouse.
The premise of installation here is that your computer must have Maven installed and configure the Maven environment variables
Local Maven installation directory

Maven environment variables
M2_HOME

MAVEN_OPTS (configure this parameter, Maven builds the project faster and better)

path

Since my IDEA is configured with local Maven, I can use IDEA to manually add the JDBC driver dependency of the Oracle database to the Maven local repository.
Above we have found the Oracle driver package location:
E:\Oracle\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar
However, if the file path of the Maven build jar package is directly pointed to this directory, it may appear that Oracle will deny access, then we can copy ojdbc6.jar to a directory, I chose E:\Java\idea\ssh\ojdbc6. jar

Use Maven's add dependency (install) command:
mvn install:install-file
-Dfile=E:\Java\idea\ssh\ojdbc6.jar
-DgroupId=com.oracle
-DartifactId=ojdbc6
-Dversion=11.2.0.1.0
-Dpackaging=jar
We can use various tools to install to the Maven repository, such as the DOS command window, Eclipse's Maven plug-in, IDEA's Terminal, the tools are different, but the operation is the same, the specified file location and Maven coordinates are the same, I choose is IDEA's Terminal

We see that Maven has installed this jar package into the local Maven repository and find this repository directory:

We add the following Oracle driver dependencies to the Maven project
<!-- Add oracle driver dependency-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
The Maven coordinates must be the same as when building the Oracle-driven Maven dependency by yourself, otherwise an import error will be reported.
Since I've been practicing SSH projects recently, add dependencies to the project:

We can find it directly in the Maven dependency graph:

Let's test the JDBC driver of the Oracle database
Since I used the c3p0 connection pool to configure the data source in the project, the obtained database connection Connection instance is wrapped by the c3p0 connection pool, that is, obtained from the database connection pool.
applicationContext.xml section configuration

jdbc.properties properties file

test class

Just write it here, learning this thing is to be diligent and sleep! ! !

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325335085&siteId=291194637