Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

need clarification on sql query written in . java file

Posted on 2014-01-27
7
Medium Priority
?
539 Views
Last Modified: 2014-02-05
hi  everyone i have query in my .java file these kind of queries basically we are using to fetch some records from db.
i want to understand these kind of queries written in many places.
is there any siter to understand such kind of queries?

here is the query:
String sql = "SELECT ticket FROM Destination ticket "
                              + " WHERE ticket.sendAttempts < :threshold AND "
                              + " ticket.currentStatus.distributionType = :distType AND (";
                  
                  for (int i = 0; i < statuses.size(); i++) {
                        sql += "ticket.currentStatus.ticketStatusType=:status" + i;
                        if (i != statuses.size() - 1)
                              sql += " OR ";
                  }
                  sql += ")";





can any one explain what this query will do and how can i learn such kind of queries
For me it is difficult to understand.
Thanks.
0
Comment
Question by:srikotesh
7 Comments
 
LVL 3

Assisted Solution

by:smilieface
smilieface earned 400 total points
ID: 39814213
The string you have there will construct a SQL query that will return a collection of rows stored in a table Destination.

The reference "tickets" is an alias to the destination.

The "for" section is constructing an list of ticket statuses which are to be included.

I don't recognise what is happening to the "ticket.currentStatus.distributionType" as a 3 part name for a column isn't a regular SQL construction.
0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 200 total points
ID: 39814223
More generally if you're looking to learn SQL, there are plenty of sites out there.

For example try this one:
http://www.sqlcourse.com

specifically this part deals with select statements:
http://www.sqlcourse.com/select.html

which is the kind of query you posted.

Hope that helps,

Doug
0
 
LVL 6

Accepted Solution

by:
Mahesh Bhutkar earned 1000 total points
ID: 39817163
It seems that you are using Hibernate HQL here.

This is complex hql example which contains conditional joins on currentStatus object.
Here currentStatus has association with Destination.

It returns ticket as object with conditions
1) ticket.sendAttempts < :threshold
2) ticket.currentStatus.distributionType = :distType
   Join with Destination & currentStatus and condition on distributionType
3) ticket.currentStatus.ticketStatusType=:status
   Join with Destination & currentStatus and condition on ticketStatusType

You can refer HQL for more clarification from, http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Assisted Solution

by:tliotta
tliotta earned 400 total points
ID: 39817430
Also, note that you're not actually asking about SQL nor even about a "query". What you're asking about is programming a loop, which is very different from SQL or a query. The loop is simply building a string named "sql" by adding characters to it each time through the loop.

It's only when the loop finishes and passes the sql variable to a query engine that a query gets involved.

The best way to learn what it's doing is to look at the sql String variable each time the loop runs. That might be done in a debug session, or by adding a statement in the loop to display or print the variable.

In order to explain a "query", we would need to see the final version of the statement that gets built (though it's pretty clear what's happening) and we'd have to know what query engine is used.

Tom
0
 
LVL 2

Author Comment

by:srikotesh
ID: 39828986
thanks bonzermahesh.

hi tliotta , i am using db2,and one more thing is i want to practice such kind of queries can you suggest any advice, the above urlsi have seen so far is all are sql queries whatever i posted that  is some thing  different  from sql query that is not simple sql query.

if it is like that i can search in google then i can understand.
0
 
LVL 6

Assisted Solution

by:Mahesh Bhutkar
Mahesh Bhutkar earned 1000 total points
ID: 39829541
Using Java programming you are creating here dynamic HQL query.
0
 
LVL 2

Author Closing Comment

by:srikotesh
ID: 39835821
Thanks for your valuble suggestions
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses

971 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