Spring 的 Oracle 数据源配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13388045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:31:42  来源:igfitidea点击:

Oracle data-source configuration for Spring

oraclespringdatasource

提问by mauryat

In the Spring framework, how is an Oracle data-source configured?

在 Spring 框架中,如何配置 Oracle 数据源?

回答by mauryat

In the context.xmlfile:

context.xml文件中:

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
    <property name="dataSourceName" value="ds"/>
    <property name="URL" value="jdbc:oracle:thin:@<hostname>:<port_num>:<SID>"/>
    <property name="user" value="dummy_user"/>
    <property name="password" value="dummy_pwd"/>
</bean>

Example of URL:jdbc:oracle:thin:@abc.def.ghi.com:1234:TEAM4

URL 示例:jdbc:oracle:thin:@abc.def.ghi.com:1234:TEAM4

回答by ukchaudhary

1. Since Oracle JDBC Driver is not in Maven repository, download it from http://www.oracle.com/technetwork/database/features/jdbc/default-2280470.html(for example Oracle Database 12.1.0.2 JDBC Driver) and add this driver through Maven command as follows:

1. 由于 Oracle JDBC Driver 不在 Maven 存储库中,请从http://www.oracle.com/technetwork/database/features/jdbc/default-2280470.html下载 (例如 Oracle Database 12.1.0.2 JDBC Driver)和通过 Maven 命令添加此驱动程序,如下所示:

(in my case)

(在我的情况下)

mvn install:install-file -Dfile=D:\Downloads\Java\ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2 -Dpackaging=jar

2. Add in pom.xml

2.在pom.xml中添加

 <dependency>
     <groupId>com.oracle</groupId>
     <artifactId>ojdbc7</artifactId>
     <version>12.1.0.2</version>
 </dependency>

3. Add in application.properties file

3.在application.properties文件中添加

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=hr
spring.datasource.password=hr
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

回答by raghera

Note you may want to add to the above that the Oracle driver does not have an open source licence so it will not be in the Maven central repository. You'll have to add it to your local repo.

请注意,您可能希望补充上述内容,即 Oracle 驱动程序没有开源许可证,因此它不会出现在 Maven 中央存储库中。您必须将其添加到本地存储库中。

To do this: Get the driver you want from: http://www.oracle.com/

为此:从以下网址获取所需的驱动程序:http: //www.oracle.com/

Or you can get it from your oracle installation: {ORACLE_HOME}\jdbc\lib\ojdbc6.jar

或者您可以从您的 oracle 安装中获取它:{ORACLE_HOME}\jdbc\lib\ojdbc6.jar

Then run the following maven command:

然后运行以下 maven 命令:

mvn install:install-file -Dfile={Path/to/your/ojdbc.jar} -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar

This should install it in your local repository so when you reference it as user640378 states above it should work correctly.

这应该将它安装在您的本地存储库中,因此当您以上面的 user640378 状态引用它时,它应该可以正常工作。

回答by Fredi Vázquez Hilerio

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="dataSourceName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="URL" value="jdbc:oracle:thin:@localhost:1521:XE" />
    <property name="username" value="hr" />
    <property name="password" value="hr" />
    <property name="initialSize" value="1" />
    <property name="maxActive" value="5" />
</bean>