Getting number of "?" out of the dql query

Posted on 2006-04-30
Last Modified: 2008-03-17
I have a text box, in which user's come and type in the dql statement like “select * from dm_document where owner_name = ?  and object_name=?". I need to get the count of all the "?" marks in the query. Like in this case it was 2 questions marks. How can i accomplish this task in my java class.
Question by:janjuama84
    LVL 17

    Accepted Solution

    If they just type in a string, you can keep a running tally and keep searching:

    String dql = "select * from dm_document where owner_name = ?  and object_name=?";
    int count = 0;
    int pos = dql.indexOf("?");
    while (pos != -1)
      pos = dql.indexOf("?", pos+1);
    // after the loop, count has the number of question marks.

    LVL 17

    Expert Comment

    A little more compact, into a for loop:

    String dql = "select * from dm_document where owner_name = ???  and object_name=?";
    int count = 0;
    for(int pos = dql.indexOf("?"); pos != -1; pos = dql.indexOf("?", pos+1))

    Joe P
    LVL 92

    Expert Comment

    PreparedStatement ps = conn.prepareStatement(dql);
    int count = ps.getParameterMetaData().getParameterCount();

    LVL 23

    Expert Comment

    check out.

    yes getting it from the getParameterMetaData is a good way.

    Thank You.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    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 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 …

    755 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

    19 Experts available now in Live!

    Get 1:1 Help Now