Solved

about  method in  java

Posted on 1998-04-24
3
173 Views
Last Modified: 2010-04-16
how to write method to get JDBC statment used for executing a JDBC SQL statement
0
Comment
Question by:mera
3 Comments
 
LVL 5

Expert Comment

by:fontaine
ID: 1219523
What you want exactly is not clear to me. You should be more verbose in your questions. Are you happy if I post a sample app. that connects to a database, sends it a SQL statement and displays the results that are obtained?
0
 

Accepted Solution

by:
mka earned 100 total points
ID: 1219524
Hi,

From what I make out of your second question, (tell me if I am wrong) you want to get back the SQL statement of the java.sql.Statement. I can give you two options for its solution:
a) Store the SQL statement in a string, and pass the string alongwith the java.sql.Statement to your methods.
.....
String myString=new String("select * from mytable");
//assuming statement declaration is done
ResultSet resultSet=statement.executeQuery(myString);
//so, wherever you need the statement's text, you have it in the string. If you want it more dynamic, you may have to store a dynamic array of such strings for the statements generated throughout the program. If you are writing some sort of API, I could give a much more complex, yet flexible solution.
b) In this method you cannot exactly recover the SQL string, but you can get to all the details of the statement. After obtaining the resultset, get the meta-data for the statement. This is simple and you can get all the column names, column lengths, table names etc., etc. However, you'd have to revert to solution 'a' if there are SQL statements which do not give results and you execute with statement.executeUpdate(). Hope I am clear.

Write in these columns to contact me. Also, I'll be notified by email if you (or anyone) writes a comment or answer to your question.

Thanks.
Kaleem (mka).
0
 

Author Comment

by:mera
ID: 1219525
Thanks for giving me 100 quality points!!! (I feel like a child again, who becomes happy on seeing some coins.)

First, you should tell yourself that Statement.getStatement() cannot be done without playing with Java's Core API (java.sql.*).

To get around the problem, there are several solutions. One of them would be - declaring your own class (say SQLStatement) and using that class to

get to all the methods of java.sql.Statement (like getString() being called as SQLStatement.getString()); but simple though it might seem - I'd

stay away from it. There are more catch here than in my next solution.

I'll be assuming that you are writing one class (as against the package you must be writing).
Also, I'll be using object names instead of class names -- it shouldn't confuse you if I directly say
statement.getString (since I assume I have declared "java.sql.Statement statement;" already).

Declare two global vector objects like this:
// If you prefer not to put them in the same class you use;
// you will have to put up an API definition and accomodate a class for these
// (which I guess you wouldn't bother right now)
public static Vector vectorOfStatements=new Vector();         // To store the statement instances as they are used
public static Vector vectorOfStatementStrings=new Vector();   //

Whenever and wherever you make the call to -
    statement.executeQuery("some SQL string"); or statement.executeUpdate("some SQL string");
do these instead:
    String stmtString=new String("some SQL string")
    statement.executeQuery(stmtString);     or    statement.executeUpdate(stmtString);
    setSQLStatementString(statement,stmtString); // you'll write this method; I'll tell how

When you want to do your getStatement, you'll have to say:
    String mySQLStatementWas=getSQLStatementString(statement); // you'll have to write this method on the RHS; I'll tell how

// For an API, you'd prefer to return boolean from this method -- to take care of everything
public void setSQLStatementString(Statement stmt, String stmtString)
{
   // First search whether the statement instance already exists. If it does then update the string, otherwise create a new entry.
   for (Enumeration e=vectorOfStatements.elements(); e.hasMoreElements(); e.nextElement())
   {
   // You'll not need this checking if for each SQL statement that you use, you do not re-use a previously defined java.sql.Statement.
   // Please don't say later I did not warn you: If you do not implement this for-loop it will work fine. But you'll have to work such that
   // for every new SQL statement, you'll have to define it newly. i.e., Each time you must say "Statement statement;" and use "statement".
   // Defining once and using the "statement" instance will cause problems without this for-loop.
   }

   // Create a new entry
   vectorOfStatements.addElement(stmt);
   vectorOfStatementStrings.addElement(stmtString);
}

public String getSQLStatementString(stmt)
{
   int i=0;
   // First search for the statement instance that was stored in the vector
   for (Enumeration e=vectorOfStatements.elements(); e.hasMoreElements(); )
   {
        i++;
        // compare the two statements whether they are the same
        if ((Statement) e.nextElement() == stmt)
        {
            // You got the statement for which you want to locate the SQL String
            return vectorOfStatementStrings.getElementAt(i);  // If the string contains nothing, you'll be returned a 'nothing'
                                                              // (but not null - since first it will go to nullPointerException!)
        }
    }

}

You might find my solution silly; but I did not have time to devise the fully tested and working solution as I would want it to be.

Thanks.
Kaleem (mka).
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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

757 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

17 Experts available now in Live!

Get 1:1 Help Now