I'm trying to execute a sql query and try to get its value in a integer variable, but I'm getting a compile time error saying
The method queryForInt(String) is undefined for the type JdbcTemplate
My code is correct I think, so I have problem in my pom file.
my 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>
<spring.version>4.1.0.RELEASE</spring.version>
</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-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
</dependencies>
</project>
my app.java file:
package sql.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
//import org.springframework.jdbc.core;
import java.util.List;
import javax.sql.DataSource;
//import org.springframework.jdbc.datasource.DriverManagerDataSource;
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 )
{
//DataSource dataSource = null ;
ApplicationContext context =
new ClassPathXmlApplicationContext("web.xml");
DataSource obj = (DataSource) context.getBean("dataSource");
JdbcTemplate jdbcTemplateObject = new JdbcTemplate(obj);
String SQL1 = "select count(*) from issues";
int row1 = jdbcTemplateObject.queryForInt(SQL1);
System.out.println(row1);
System.out.println( "Hello World!" );
}
}
queryForList returns a List of LinkedHashMap objects.
A usage example with JdbcTemplate: JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object RowCountCallbackHandler countCallback = new RowCountCallbackHandler(); // not reusable jdbcTemplate. query("select * from user", countCallback); int rowCount = countCallback. getRowCount();
JdbcTemplate "queryForObject" and "query" is deprecated in Spring.
[1] 'queryForObject()' can return not null.
The method JdbcTemplate.queryForInt
was deprecated in Spring 3.2.2 and it was removed in Spring 4.2.0.
There is actually a dependency conflict in your pom.xml
: you depend on Spring version 4.1.0.RELEASE
and 4.2.0.RELEASE
(for spring-jdbc
). Maven solves that conflict by using version 4.2.0.RELEASE
for all Spring dependencies, so that is why the method queryForInt
is not available.
You can :
4.1.0.RELEASE
for all your dependencies4.2.0.RELEASE
and use instead queryForObject(String sql, Class<T> requiredType)
with Integer.class
as requiredType
.I recommend you to upgrade as it is not a good practice to continue using deprecated APIs.
See this question for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With