spring MVC Ошибка веб-приложения при попытке подключения к Oracle: Несоответствие типов: невозможно преобразовать DriverManagerDataSource в DataSource - PullRequest
0 голосов
/ 31 января 2020
  I am working on a Spring MVC web app and trying to simply connect to an Oracle database 
  (to select the job status info stored in a table). I carefully installed Oracle 
   ojdbc7.jar into the Maven respository and added the dependency into my pom.xml, created 
  the DAO class, DAOimpl class and added the following into my WebConfig class:

  public DataSource dataSource() {
      DriverManagerDataSource ds = new DriverManagerDataSource();

   ds.setDriverClassName(oracle.jdbc.driver.OracleDriver.class.getName());
   ds.setUrl("jdbc:oracle:thin:@<server>:1521:<sid>");
   ds.setUsername("user");
   ds.setPassword("*****");
   return ds;

   But WebConfig will not compile because of: Type mismatch 
   on ds: cannot convert from 
   DriverManagerDataSource to DataSource

   Has anyone experienced this issue? And what was the solution?

Дайте мне знать, если вам нужна дополнительная информация (и, пожалуйста, будьте добры - я java новичок ie)

Кстати, для создания пружины MVC CRUD, я посетил следующие сайты: https://www.codejava.net/frameworks/spring-boot/spring-boot-crud-web-application-with-jdbc-thymeleaf-oracle

https://javainterviewpoint.com/spring-crud-example-jdbctemplate/

https://www.javatpoint.com/spring-mvc-crud-example

Спасибо

Вот мой пом:

<dependencies>
<!-- Spring MVC Dependency -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.3.7.RELEASE</version>
</dependency>

<!-- JSTL Dependency -->
<dependency>
  <groupId>javax.servlet.jsp.jstl</groupId>
  <artifactId>javax.servlet.jsp.jstl-api</artifactId>
  <version>1.2.1</version>
</dependency>

<dependency>
  <groupId>taglibs</groupId>
  <artifactId>standard</artifactId>
  <version>1.1.2</version>
</dependency>

<!-- Servlet Dependency -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

<!-- JSP Dependency -->
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>javax.servlet.jsp-api</artifactId>
  <version>2.3.1</version>
  <scope>provided</scope>
</dependency>


<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ora-jdbc</artifactId>
  <version>7</version>
</dependency>

<dependency>  
  <groupId>org.springframework</groupId>  
  <artifactId>spring-jdbc</artifactId>  
  <version>5.1.1.RELEASE</version>  
</dependency>   

 </dependencies> 

<build> 
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
  <resource>
    <directory>src/main/resources</directory>
  </resource>
</resources>
<plugins>

<plugin> 
  <groupId>com.googlecode.addjars-maven-plugin</groupId> 
  <artifactId>addjars-maven-plugin</artifactId> 
  <version>1.0.5</version> 
  <executions> 
    <execution> 
    <goals>  
      <goal>add-jars</goal> 
    </goals> 
    <configuration> 
    <resources> 
      <resource> 
        <directory>${basedir}/lib</directory> 
        <includes> 
          <include>**/*.jar</include> 
        </includes> 
      </resource> 
    </resources> 
    </configuration> 
    </execution> 
  </executions> 
</plugin> 

<!-- Embedded Apache Tomcat required for testing war -->
<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <configuration>
    <path>/</path>
  </configuration>
</plugin>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <failOnMissingWebXml>false</failOnMissingWebXml>
  </configuration>
</plugin>

</plugins>
</build>
</project>

1 Ответ

1 голос
/ 31 января 2020

Как свидетельствует ошибка, в вашем методе datasource() существует несоответствие типов между Datasource и DriverManagerDataSource.

Убедитесь, что вы импортируете правильную зависимость.

Datasource должно быть: javax.sql.DataSource и DriverManagerDataSource должно быть: org.springframework.jdbc.datasource.DriverManagerDataSource.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...