0

i am trying to insert data into a database using 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:

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:

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 file:

<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>
3
  • 4
    What makes you think a DriverManagerDataSource is an instance of App? Even if you fixed that your code will fail with the JdbcTemplate as you aren't putting the DataSource into it, hence it will fail. I strongly suggest a tutorial or 2 to understand how to use those classes. Commented Aug 19, 2015 at 9:34
  • because app is trying to get access to mysql database Commented Aug 19, 2015 at 9:36
  • Do you think DriverManagerDataSource extends your App class? I strongly suggest a read on class inheritance and how that works... You seem to be missing basic understanding of that. Commented Aug 19, 2015 at 9:38

3 Answers 3

3

There are multiple things wrong with your code.

First a DriverManagerDataSource is a DataSource NOT an instance of your App class. (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 jdbcTemplateObject = new JdbcTemplate(ds);

I do suggest however you add the JdbcTemplate to your configuration and retrieve that instead of the 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). The xml contained a reference to the 3.0 xsd of beans whereas your pom uses a 2.5 version of Spring.

Sign up to request clarification or add additional context in comments.

Comments

2

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!" );
        }
    }

Comments

1

context.getBean("dataSource"); returns a DataSource not an Object of class App.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.