Solved

selectAndOrderPs.setString(1, visitId)

Posted on 2004-10-15
5
250 Views
Last Modified: 2012-06-21
   
Hi!

Can anybody tell me what selectAndOrderPs.setString(1, visitId) does in detail?
++++++++++++++

private String selectTitlesAndOrder="select test.assess_test_id as id, type.test_type_title as title"+ " FROM Assess_Tests test, Assess_test_types type" +" WHERE visit_id=? AND" + " test.assess_test_type_id=type.assess_test_type_id" + " ORDER BY ?";


public LinkedHashMap selectTitlesAndOrder(String visitId, String orderParam)
       throws SQLException {
            log.debug("start");

            LinkedHashMap testMap = new LinkedHashMap();
            
            selectAndOrderPs = db.prepareStatement(selectTitlesAndOrder);
            selectAndOrderPs.setString(1, visitId);
            selectAndOrderPs.setString(2,orderParam);

            ResultSet rs = selectAndOrderPs.executeQuery();
      
            while(rs.next()) {
                  testMap.put(rs.getString("id"), rs.getString("title"));
            }

            selectAndOrderPs.close();
            
            return testMap;
    }
++++++
thanks,
0
Comment
Question by:dkim18
5 Comments
 
LVL 20

Accepted Solution

by:
Venabili earned 43 total points
ID: 12322528
Hi dkim18,
Sets the value written in visitId at the place of the first ?. It also escapes the value if needed....

Venabili
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 41 total points
ID: 12322538
It's a wrapper around a query that parameterizes a query based on its id and sort order. A Map is built such that the test assess id returned by the query is mapped to the titles of the tests
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 41 total points
ID: 12323026
The setString(n, str) set the parameter at location n to the string str.  This assumes that the statement has been prepared with parameters and that the ? position is mapped to a string.  If not a SQLException will occur.

In your example the SQL statment has parameters on 'visit_id=?', and 'ORDER BY ?'.  The 1st is valid and is parameter one.  The second will not work.  The reason is that the database uses the parameterized SQL statment and generates its internal Query.  If the statement has been used before, the database may choose to cache the statement compilation and thus make your query faster.  You cannot set the parameter on ORDER BY because the database needs to know how to sort the results BEFORE it is executed.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

22 Experts available now in Live!

Get 1:1 Help Now