繁体   English   中英

线程“主”中的异常java.lang.ClassCastException:不能强制转换org.springframework.jdbc.datasource.DriverManagerDataSource

[英]Exception in thread “main” java.lang.ClassCastException: org.springframework.jdbc.datasource.DriverManagerDataSource cannot be cast

我正在尝试使用jdbc template将数据插入数据库。 我在该表的mysql数据库中使用数据库frr,以下问题是我编写的代码,但出现以下错误:

Exception in thread "main" java.lang.ClassCastException: org.springframework.jdbc.datasource.DriverManagerDataSource cannot be cast to sql.sql.App
    at sql.sql.App.main(App.java:25)

App.java文件:

package sql.sql;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;


public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("web.xml");
        App obj = (App) context.getBean("dataSource");
         JdbcTemplate jdbcTemplateObject = new JdbcTemplate();
        String SQL = "insert into issues(issue,status,comment) values (?, ?, ?)";
        jdbcTemplateObject.update( SQL, new Object[]{"Zara", "test", "123"} );
        System.out.println( "Hello World!" );
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/frr"/>
   <property name="username" value="root"/>
   <property name="password" value=""/>
</bean>

</beans>

pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>sql</groupId>
  <artifactId>sql</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>sql</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>


    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>

  </dependencies>
</project>

您的代码有很多错误。

首先, DriverManagerDataSourceDataSource不是App类的实例。 (我建议对类继承进行一些搜索)。

DataSource ds = context.getBean("dataSource", DataSource.class);

其次,如果已解决该问题,则您的代码将无法在构造JdbcTemplate或执行查询时失败,因为JdbcTemplate需要DataSource ,因此无法凭空操作。

JdbcTemplate jdbcTemplateObject = new JdbcTemplate(ds);

我确实建议您将JdbcTemplate添加到您的配置中,然后检索它而不是DataSource

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/frr"/>
       <property name="username" value="root"/>
       <property name="password" value=""/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

然后将您的主要代码更改为以下内容。

public class App {

    public static void main( String[] args ) {

        ApplicationContext context =  new ClassPathXmlApplicationContext("web.xml");
        JdbcTemplate jdbcTemplateObject = context.getBean(JdbcTemplate.class);
        String SQL = "insert into issues(issue,status,comment) values (?, ?, ?)";
        jdbcTemplateObject.update( SQL, new Object[]{"Zara", "test", "123"} );
        System.out.println( "Hello World!" );
    }
}

注意:我也怀疑您的xml确实在工作(或者pom是您使用的pom)。 xml包含对3.0 xsd bean的引用,而pom使用2.5版本的Spring。

context.getBean("dataSource"); 返回一个DataSource而不是App类的Object。

DataSource obj = (DataSource) context.getBean("dataSource");
JdbcTemplate jdbcTemplateObject = new JdbcTemplate(obj);

如下修改您的代码

public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext context = 
                    new ClassPathXmlApplicationContext("web.xml");
            DataSource obj = (DataSource) context.getBean("dataSource");
             JdbcTemplate jdbcTemplateObject = new JdbcTemplate(obj);
            String SQL = "insert into issues(issue,status,comment) values (?, ?, ?)";
            jdbcTemplateObject.update( SQL, new Object[]{"Zara", "test", "123"} );
            System.out.println( "Hello World!" );
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM