Opening db connections

  • Thread starter =?ISO-8859-1?Q?Eduardo_Y=E1=F1ez_Parareda?=
  • Start date
?

=?ISO-8859-1?Q?Eduardo_Y=E1=F1ez_Parareda?=

Hi,

when the connection to a DB is open, when you get the Statement or when you execute it?

ie.:

Statement stmt = StatementProvider.getStatement();
ResultSet rs = stmt.executeQuery("select 1");
rs = stmt.executeQuery("select 1");
rs = stmt.executeQuery("select 1");

Does this code mean 3 opened connection or only one (getStatement)?
 
L

Lew

Eduardo said:
when the connection to a DB is open, when you get the Statement or when
you execute it?

When you issue a DriverManager.getConnection(), typically, or via the
javax.persistence.EntityManager class.
ie.:

Statement stmt = StatementProvider.getStatement();
ResultSet rs = stmt.executeQuery("select 1");
rs = stmt.executeQuery("select 1");
rs = stmt.executeQuery("select 1");

Does this code mean 3 opened connection or only one (getStatement)?

It's no more than one, but not being familiar with the StatementProvider class
it's hard to be sure.

What is StatementProvider?
 
G

Guest

It's no more than one, but not being familiar with the StatementProvider
class it's hard to be sure.

What is StatementProvider?

I'm sorry, it's a class of my project, I didn't realize of that.

This is what I meant:

Connection conn = .... getConnection...

Statement stmt = conn.getStatement();
ResultSet rs = stmt.executeQuery("select 1");
rs = stmt.executeQuery("select 1");
rs = stmt.executeQuery("select 1");
 
L

Lew

I'm sorry, it's a class of my project, I didn't realize of that.

This is what I meant:

Connection conn = .... getConnection...

Statement stmt = conn.getStatement();
ResultSet rs = stmt.executeQuery("select 1");
rs = stmt.executeQuery("select 1");
rs = stmt.executeQuery("select 1");

You have as many connections as conn points to.
<http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html#getConnection(java.lang.String)>

<http://java.sun.com/javase/6/docs/api/java/sql/Connection.html>

and, of course, the Javadocs for ResultSet and Statement.

BTW, consider using a PreparedStatement instead of a "regular" Statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,830
Latest member
HeleneMull

Latest Threads

Top