0

I have got a program this way:

public void MethodOne()
{
    String sqlquery = "select * from vendor_items where category_id = 1 ";
    PreparedStatement consildatedPst = connection.prepareStatement(sqlquery);
    ResultSet consilatedReslset = consildatedpst.executeQuery();
    while(consilatedReslset.next())
    {
        String name = consilatedReslset.getString("name");
        if(name!=null)
        {
            MethodTwo();
        }
    }
}

public void MethodTwo(String name)
{
    String sqlquery2 = "select ename from Vendor where name=?";
    PreparedStatement otherPst = connection.prepareStatement(sqlquery2);
    otherPst.setString(1,name);
}

This is the way connection is established (Later I will go for Connection Pooling).

public class DBConnection {

    public static Connection getDBConnection() {
        String sURL="jdbc:mysql://localhost:3306/oms";
        String sUserName="root";
        String sPwd="";
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(sURL, sUserName,sPwd);
            return conn;
            } catch (SQLException e) {
            e.printStackTrace();
            } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }

}

My question is Can I use the same connection object when calling within Methods??

6
  • 2
    Not unless you finish with the connection first, by reading the entire ResultSet for example. Otherwise, your ResultSet will no longer be tied to an active cursor. Jul 6, 2014 at 4:26
  • why dont u store the names from the resultset in some list and later iterate the list and call the method
    – SparkOn
    Jul 7, 2014 at 10:41
  • @ElliottFrisch why? there are two different ResultSets from 2 different PreparedStatements.
    – inigoD
    Jul 10, 2014 at 9:11
  • @Eddie one statement is usually backed by one cursor. One connection per cursor. It's how databases usually work. Jul 10, 2014 at 17:28
  • @ElliottFrisch JDBC4 spec. says that you can use different Statements from the same connection concurrently (Point 13.1.1 in download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/…), I've done it many times in postgresql and know that in oracle you can configure the maximum number of cursors per connection. And BalusC says so here (Argument from authority I know: logical fallacy):stackoverflow.com/questions/5149135/… so IMHO you are wrong.
    – inigoD
    Jul 11, 2014 at 7:36

2 Answers 2

1

Yes, you can.

When you do:

connection.prepareStatement(sqlquery2);

It creates a new statement object using the same connection. So the ResultSets that you obtain from them will belong to different Statements and will be different and there will be NO PROBLEM for you.

In short: Different Statements manage different ResultSets. If you get 2 ResultSets from the same Statement when you get the second one the first one will be dropped but if you have 2 Statements you can manage 2 ResulSets without problem (while the connection is open, of course)

0

Only if you aren't using the connection in multiple threads or nesting your own methods. In other words, no. Use a new connection per method. To avoid overhead use a connection pool.

1
  • Why not? He's not using threading -JDBC don't guarantee thread-safety in connections- not even updating the DDBB -that could change your query's response-. What's wrong with using the same connection in nested methods with different preparedstatements?
    – inigoD
    Jul 10, 2014 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.