Jump to content

Java - Statement (JDBC)

Hi P
Go to solution Solved by Jisagi,

It doesn't really matter which way you do it. There are a lot of good online tutorials which suggest the one or the other way. The official oracle java doc lists a new Statement object inside a method, but since the example only displays one method and not the whole class, it might only be for the examples sake. In the end, both ways will work just fine.

 

The connection object on the other hand can/should be held as class or instance variable (or as method argument) and not inside each new method. If you want Statements on a (pure) local scope inside each method, you could use PreparedStatements and omit normal Statements completely.

 

 

Say we have a Class to handle a database, and this class has 2 methods, one to INSERT and another to DELETE

 

Do I have to create an Statement in each of these and close them after executing a query??

(short example below, ignore the empty catch)

 

Spoiler

public void addValue() {
    try (Statement statement = connection.createStatement()) {
        statement.execute(" QUERY ");
    } catch (SQLException e) {}
}

// imagine another identical method

 

 

Is that how it works?

Do I have to do this for any other method that has to interact with the database?

 

Thank you :) 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

It doesn't really matter which way you do it. There are a lot of good online tutorials which suggest the one or the other way. The official oracle java doc lists a new Statement object inside a method, but since the example only displays one method and not the whole class, it might only be for the examples sake. In the end, both ways will work just fine.

 

The connection object on the other hand can/should be held as class or instance variable (or as method argument) and not inside each new method. If you want Statements on a (pure) local scope inside each method, you could use PreparedStatements and omit normal Statements completely.

 

 

Gaming Rig: Ryzen 9 5950x | 2x16GB DDR4 3200MHz | XFX Reference 6800 XT | MSI Unify X570 | Corsair MP600 2TB, Samsung 850 Evo 500GB | bequiet 850W Straight Power 11

Server: Ryzen 5 3600 | 4x32GB DDR4 ECC 2400MHz | Asrock Rack X470D4U | Samsung EVO Plus 250GB, 6x Seagate Exos 8TB, Samsung 850 Pro 1TB | bequiet 550W Straight Power 11

Link to comment
Share on other sites

Link to post
Share on other sites

I think I wrote a small library back in my database class that handles all the jdbc connection and you would just need to provide it the sql query you want to execute. I can show you if you want. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×