?
Solved

IN Statement Query

Posted on 2009-07-04
12
Medium Priority
?
248 Views
Last Modified: 2012-05-07
I have a count query which i am using to determine if the user exist or not. I am having an IN Statement in the query. Unfortunately everytime i run spring throws an error that the column value is greater than 2. If someone can point what i am doing wrong that would be great.
private final static String VALIDATE_USER_SQL = " select count(*) from user_table where id = ? and user in (?) ";
 
public boolean validateUser(final UserModel userModel, final List user) {
 
		if (LOG.isInfoEnabled()) {
			LOG
					.info("inside validateUser()");
		}
 
		Object[] parameters = new Object[] { new Integer(userModel.getUserId(), user };
		int[] paramTypes = { Types.INTEGER, Types.VARCHAR };
		int count = 0;
		try {
 
			count = getJdbcTemplate().queryForInt(VALIDATE_USER_SQL,
					parameters, paramTypes);
 
			if (count > 0) {
 
				if (LOG.isDebugEnabled()) {
					LOG.debug("Count is [ " + count + " ]");
				}
 
				return true;
			}
 
		}
 
		catch (final DataAccessException ex) {
 
			if (LOG.isErrorEnabled()) {
				LOG.error("DataAccessException [ " + ex.getMessage() + " ]");
			}
		}
 
		return false;
	}

Open in new window

0
Comment
Question by:Micheal_Male
[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
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 24777487
You can't parameterize (use bind variables in) a set unfortunately
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24777508
You did not say which database; if you are using Oracle you can pass Java collections in as bind variables.  You cannot use a direct IN(:var) but you can pass it as a collection and cast it.

http://knol.google.com/k/franck-pachot/oracle-passing-a-list-as-bind-variable/17uabcrki6uux/2#
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 900 total points
ID: 24777510
I agree.
however, if you have for example sql server, you could implement a function like I did (search for dbo.ParmsToList), and change the query like this:
private final static String VALIDATE_USER_SQL = " select count(*) from user_table where id = ? and user in ( select value from dbo.ParmsTolist(?, ',') ) ";

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24777529
Otherwise, if you can't use that or don't want to narrow the portability of your code, just concatenate the List as part of the sql string and use the other parameter as the only bind variable. Although i must say i'm a little surprised that 'getUserId' can have any relevance to more than one user ...
0
 

Author Comment

by:Micheal_Male
ID: 24777538
I am suing postgres DB
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24777555
>>I am suing postgres DB

What, for not allowing you to use a List as a bin variable..? ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24777557
(bind variable)
0
 

Author Comment

by:Micheal_Male
ID: 24777567
LOL i am sorry i meant using heheh. I will search on bind variable and will implement the solution to see if it works. Will update.
0
 

Author Comment

by:Micheal_Male
ID: 24777576
Or i can use a loop to see what the user list size is and pass the id and the user in a loop. If count is > 0 then break and return true. But i was trying to find an efficient way like Bind variables. Let me see if i can implement it.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24777599
>>I am suing postgres DB

LOL, thanks for the laugh... :)

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777623
I am also sure the function I mentioned can be translated into postgreSql ... :)
0
 

Author Comment

by:Micheal_Male
ID: 24777676
Thanks All. Angel idea is the perfect and easy one. Worked perfect,.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

719 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