Solved

need clarification on sql query written in . java file

Posted on 2014-01-27
7
515 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 100 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 26

Assisted Solution

by:dpearson
dpearson earned 50 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 250 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 27

Assisted Solution

by:tliotta
tliotta earned 100 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 1

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 250 total points
ID: 39829541
Using Java programming you are creating here dynamic HQL query.
0
 
LVL 1

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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:
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now