-4

Iam Doing my Project on Spring Frame Work. Below is the piece of code in the service class. Hey Guys can i know what is the problem with this code??

List<EventCommand> employee = (List<EventCommand>) JdbcTemplate.query(sql, new EventService());

It says

Cannot make a static reference to the non-static method query(String, RowMapper) from the type JdbcTemplate

parker
  • 3
  • 2
  • 6

3 Answers3

1

Create instance of JdbcTemplate and then call query(String, RowMapper) api.

@Autowired
JdbcTemplate jdbcTemplate;  // use spring autowiring to autowire jdbcTemplate

List<EventCommand> employee = (List<EventCommand>) jdbcTemplate.query(sql, new EventService());
atish shimpi
  • 4,873
  • 2
  • 32
  • 50
0

query isn't static. You need a new JdbcTemplate(), or similar. Consult the JavaDoc.

You should be able to work this out from the error. I would recommend spending more time with the basics of Java if you can't.

Jxek
  • 502
  • 1
  • 4
  • 13
  • Thanks for the opinion.I have created an object for the JdbcTemplate but if forgot to see it. – parker Jan 15 '15 at 15:07
0

First you need DataSource. You can read about it here

http://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html

Next you need to create JdbcTemplate

http://docs.spring.io/spring-framework/docs/2.0.x/api/org/springframework/jdbc/core/JdbcTemplate.html

and use it in your query

List<EventCommand> employee = (List<EventCommand>) (jdbcTemplate).query(sql, new EventService());
Vitaly
  • 2,552
  • 2
  • 18
  • 21