samedi 2 juillet 2016

MySQL open every time a new Statement?

I'm working with MySQL for long time now and I know that I should only open one Connection at one time.

But now I have an other question that I think is not asked yet. If so please don't blame me, because I couldn't find an answer yet.

Every time I run a MySQL-Query or Update I open a new Statement like this:

Statement st = c.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM ...;");
...
st = c.createStatement();
st.executeUpdate("INSERT INTO ...;");
...
st = c.createStatement();
res = st.executeQuery("SELECT * FROM ...;");
...

First of all. Is it possible to create just one Statement for all the Querys and Updates? Like this:

Statement st = c.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM ...;");
...
st.executeUpdate("INSERT INTO ...;");
...
res = st.executeQuery("SELECT * FROM ...;");
...

And the other question I have is, if I have to close these Statements in the end or if Java does this automatically.

Aucun commentaire:

Enregistrer un commentaire