[英]Exception in thread “main” java.lang.ClassCastException: org.springframework.jdbc.datasource.DriverManagerDataSource cannot be cast
i am trying to insert data into a database using jdbc template . 我正在尝试使用jdbc template将数据插入数据库。 i am using database frr in mysql database in that table issues below is my code that i have wrote but i am getting below error: 我在该表的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 File: 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 : 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 file: 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>
There are multiple things wrong with your code. 您的代码有很多错误。
First a DriverManagerDataSource
is a DataSource
NOT an instance of your App
class. 首先, DriverManagerDataSource
是DataSource
不是App
类的实例。 (I suggest a little googling on class inheritance). (我建议对类继承进行一些搜索)。
DataSource ds = context.getBean("dataSource", DataSource.class);
Second if that is fixed your code would fail on either constructing the JdbcTemplate
or on executing the query, because a JdbcTemplate
needs a DataSource
it cannot operate on thin air. 其次,如果已解决该问题,则您的代码将无法在构造JdbcTemplate
或执行查询时失败,因为JdbcTemplate
需要DataSource
,因此无法凭空操作。
JdbcTemplate jdbcTemplateObject = new JdbcTemplate(ds);
I do suggest however you add the JdbcTemplate
to your configuration and retrieve that instead of the DataSource
. 我确实建议您将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>
Then change your main code to the following. 然后将您的主要代码更改为以下内容。
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!" );
}
}
Note: I also doubt that your xml is actually working (or that the pom is the pom you are using). 注意:我也怀疑您的xml确实在工作(或者pom是您使用的pom)。 The xml contained a reference to the 3.0 xsd of beans whereas your pom uses a 2.5 version of Spring. xml包含对3.0 xsd bean的引用,而pom使用2.5版本的Spring。
context.getBean("dataSource");
returns a DataSource not an Object of class App. 返回一个DataSource而不是App类的Object。
DataSource obj = (DataSource) context.getBean("dataSource");
JdbcTemplate jdbcTemplateObject = new JdbcTemplate(obj);
Modify your code as below 如下修改您的代码
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.