1、首先查看oracle的版本 select * from v$version;

spring-boot的爱恨情仇之链接Oracle数据库

2、pom.xml导入对应数据库的驱动JAR

        <!-- oracle -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.3.0 </version>
        </dependency>

驱动需要自己去下载这里,附带oracle 10.2.0.3.0jar的驱动https://download.csdn.net/download/name963/11008234

3、配置application.prooerties文件

spring.datasource.url: jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username = xxxx
spring.datasource.password = yyyy

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

4、编写Oracle数据的OracleController文件

spring-boot的爱恨情仇之链接Oracle数据库

@RestController
@RequestMapping("/Oracl")
public class OracleController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @RequestMapping("/gettable")
    public List<Map<String, Object>> getDbType(){
        String sql = "select * from ryxx where rownum <=10";
        List<Map<String, Object>> list =  jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : list) {
            Set<Entry<String, Object>> entries = map.entrySet( );
                if(entries != null) {
                    Iterator<Entry<String, Object>> iterator = entries.iterator( );
                    while(iterator.hasNext( )) {
                    Entry<String, Object> entry =(Entry<String, Object>) iterator.next( );
                    Object key = entry.getKey( );
                    Object value = entry.getValue();
                    System.out.println(key+":"+value);
                }
            }
        }
        return list;
    }
}

5、运行Application文件

spring-boot的爱恨情仇之链接Oracle数据库

6、成功加载在浏览器上输入http://localhost:8001/Oracl/getTable来访问你的Controller文件

页面显示

spring-boot的爱恨情仇之链接Oracle数据库

控制台显示

spring-boot的爱恨情仇之链接Oracle数据库

数据库已经连接成功大家尽情发挥吧

相关文章: