Announcement Announcement Module
Collapse
No announcement yet.
URGENT NamedParameterJdbc Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • URGENT NamedParameterJdbc

    Greetings.
    I am new to Spring so please provide as much details as you can.
    The problem is next:

    I'm trying to access a DB (mysql) and get last 3 news for every of my 2 companies present in DB.
    Code:
    	private final String NEWS_BY_COMPANY_SELECT =
    		"SELECT * " +
    		"FROM news " + 
    		"WHERE company = :company " + 
    		"ORDER BY postYear";
    	
    	private final String NEWS_LAST_BY_COMPANY_SELECT =
    		NEWS_BY_COMPANY_SELECT + " LIMIT :quantity";
    
    	@Override
    	public List<News> getLastCompanyNews(String company, String newsQuatity) {
    		logger.info("Getting last " + newsQuatity + " company news for " + company);
    		logger.debug("Walue of LAST_NEWS_QUANTITY: " + newsQuatity);
    
    		Map<String, String> namedParamMap = new HashMap<String, String>();
    		namedParamMap.put("company", company);
    		namedParamMap.put("quantity", newsQuatity);
    		List<News> news = getNamedParameterJdbcTemplate().query(
    				NEWS_LAST_BY_COMPANY_SELECT,
    				namedParamMap,
    				new NewsMapper());
    		return news;
    	}
    Nothing special I suppose. But while executing this code an error occurs with the next message:
    Code:
    PreparedStatementCallback; bad SQL grammar [SELECT * FROM news WHERE company = ? ORDER BY postYear LIMIT ?]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''3'' at line 1
    Could anyone explain what I'm missing?

    Thanks in advance.

  • #2
    Is there anyone who is able to answer my question. Guys, please, give at leas a little tip or something.

    THIS IS REALLY URGENT!

    Comment


    • #3
      This isn't a spring issue.

      You are setting limit as a string literal. It needs to be an Integer.

      Comment


      • #4
        Problem solved

        That was the case. Thanks for helping. Sorry for posting non-spring thread, I had no idea of the type of the error.

        Comment

        Working...
        X