Solved

wrong query string getting picked up java spring

Posted on 2014-01-22
6
458 Views
Last Modified: 2014-02-06
HI,
I have a following file :

package com.yatra.extremesearch.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.yatra.extremesearch.dao.ParamDao;
import com.yatra.extremesearch.model.ExtremeSearchParam;
import com.yatra.extremesearch.DBTemplate.DBTemplate;

@Repository
public class ParamDaoImpl implements ParamDao {
	
	@Autowired @Qualifier("yatraDB") DBTemplate yatraDB;
	
	@Override
	public List<ExtremeSearchParam> getReqParams() throws SQLException
	{
		List<ExtremeSearchParam> reqParams = new ArrayList<ExtremeSearchParam>();
		ResultSet rs = yatraDB.executeQuery("select * from extreme_search_param limit 0,5");
		while (rs.next()) {
			ExtremeSearchParam param = new ExtremeSearchParam();
			param.setOrigin(rs.getString("origin"));
			param.setDest(rs.getString("destination"));
			param.setMinDays(rs.getInt("min_dep_days"));
			param.setMaxDays(rs.getInt("max_dep_days"));
			param.setMinStay(rs.getInt("min_stay"));
			param.setMaxStay(rs.getInt("max_stay"));
			param.setSearchType(rs.getString("search_type"));
			param.setIsValid(rs.getString("is_valid"));
			param.setSplit(rs.getInt("split"));
		
			reqParams.add(param);
		}
				
		return reqParams;	
	}

}

Open in new window


The DBTemplate.java file is as follows :

package com.yatra.extremesearch.DBTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

public class DBTemplate{
	private static final Logger logger = Logger.getLogger(DBTemplate.class);

	DataSource dataSource;
	
	public DBTemplate(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public ResultSet executeQuery (String sql) {

		ResultSet rs = null;
		Connection conn =null;
		try {
			conn = dataSource.getConnection();
			rs = conn.createStatement().executeQuery(sql);	
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
		
		return rs;
	
	}


	public void executeUpdate(String sql) {
		Connection conn =null;
		try {
			conn = dataSource.getConnection();
			conn.createStatement().executeUpdate(sql);
			
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
		finally{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					logger.error(e.getMessage());
				}
			}
		
	}
	
	
	public void loadInfile(String dumpFile, String table){
		Connection conn =null;
		PreparedStatement ptmt = null;
		String sql = "LOAD DATA LOCAL INFILE '"+ dumpFile.replace("\\", "\\\\") +
				"' REPLACE INTO TABLE "+ table
				+" FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n'"+
				" (origin, destination, price, dep_date, arrival_date, supplier, search_type)";
		try {
			conn = dataSource.getConnection();
			ptmt = conn.prepareStatement(sql);
			ptmt.executeQuery();			
			
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
		finally{
			if(conn!=null)
				try {
					ptmt.close();
					conn.close();
				} catch (SQLException e) {
					logger.error(e.getMessage());
				}
			}
	}
	
}

Open in new window



Even thought the query written in executeQuery is :
select * from extreme_search_param limit 0,5"

When i start the spring java application in debug mode and debug the
public ResultSet executeQuery (String sql) function

I see sql = select * from extreme_search_param
in debugger eclipse mode

What could be the reason for this ?

Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39799380
You are getting result as 5 records or even result is also more.

This should not be possible. But if you are getting correct result then there might be issue with eclipse.

Please check & update.
0
 

Author Comment

by:Rohit Bajaj
ID: 39799414
result is also more.
0
 

Author Comment

by:Rohit Bajaj
ID: 39799417
Does eclipse maintains an internal cache of some kind ?
Similar thing happened to me before also but i couldn't get the reason for it.
Restarting eclipse also didnt resolve the isse. But when i restarted my system things worked sanely...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39799427
PFA image file. Tested on local machine. It works as expected. Myql Limit clause sql in Debug mode
Please restart your eclipse or system.
0
 
LVL 1

Accepted Solution

by:
Smart_Kid earned 350 total points
ID: 39802065
Make your application as a executable jar file, deploy and start. Check you are running into same erroneous situation. If not, we can confirm that the issue is with Eclipse. If yes, then there exist some issue with your code.

Kindly try.
0
 
LVL 6

Assisted Solution

by:Mahesh Bhutkar
Mahesh Bhutkar earned 150 total points
ID: 39802176
Is this problem resolved?

If not, then can you decompile DBTemplate.class file & check the sql query string,
"select * from extreme_search_param limit 0,5"
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question