Solved

is this a correct way to use java.sql.Statement?

Posted on 2004-11-01
170 Views
Last Modified: 2010-03-31
private void theMethod() throws java.sql.SQLException{
    int tmpAmount = -1;
    java.sql.Statement statement = connection.createStatement()

    java.sql.ResultSet rs = statement.executeQuery("SELECT amount FROM account_table WHERE num = '2' ");

    if(rs.next()){
        tmpAmount = rs.getInt("amount");
    }
    else{
        return;
    }

    if(tmpAmount > 0){
        statement.executeUpdate("DELETE FROM records WHERE account_num = '4'");
        statement.executeUpdate("DELETE FROM supplimental WHERE id = '4' ");
        statement.executeUpdate("UPDATE account_table SET amount = 0 WHERE num = '2'");
    }
    rs.close();
    statement.close();
}

Is it OK to reuse "statement" like this, or is there some other steps necessary?

Thanks,

Wallway
0
Question by:wallway
    2 Comments
     
    LVL 92

    Accepted Solution

    by:
    thats fine, the only thing you have to worry about is only having one result set open on a statement at a time
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    do u warry about ACID transaction? what if the second statment for example failed, do u need to rollback the whole transaction?

    I think to consider Transaction management
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    931 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

    18 Experts available now in Live!

    Get 1:1 Help Now