Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36611

Re: Eclipse doesn't like else

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Eclipse doesn't like else
Date 2017-05-12 21:22 +0000
Organization A noiseless patient Spider
Message-ID <of591p$di7$1@dont-email.me> (permalink)
References <3c792901-a05d-4a02-b058-20b9e0576095@googlegroups.com>

Show all headers | View raw


On Fri, 12 May 2017 10:44:34 -0700, Eric Douglas wrote:

> Why must it give a warning on this else condition?
> 
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.PreparedStatement;
> import java.sql.SQLException;
> 
> public class CompileMe {
>      public static void testMe() {
>           try {
>                Connection conn =
>                DriverManager.getConnection("db_connect_string",
>                "db_userid", "db_password");
>                String someCondition = "check me";
>                PreparedStatement myPrep;
>                myPrep = conn.prepareStatement("initial query");
>                myPrep.executeQuery();
>                myPrep.close();
>                if (someCondition.equals("one")) {
>                     myPrep = conn.prepareStatement("query something");
>                } else {
>                     myPrep = conn.prepareStatement("query something
>                     else");
>                }
>                myPrep.executeQuery();
>                myPrep.close();
>           } catch (SQLException e) {
>                e.printStackTrace();
>           }
>      }
> }
> 
> This example will run the same whether we have 2 calls to
> prepareStatement, or set one common string to pass into prepareStatement
> after the if, but if we write it this way Eclipse gives a warning
> "Resource leak: 'myPrep' is not closed at this location".
> 
> Take out the 3 lines above the if (prepare, execute, and close a
> different query) and the warning goes away.

I'd probably pull the repeated JDBC statements out into a separate 
method, something like:

private void doQuery(String query) throws SQLException
{
  try
  {
    PreparedStatement myPrep;
    myPrep = conn.prepareStatement(query);
    myPrep.executeQuery();
    myPrep.close();
  }
  catch (SQLException e)
  {
    /*
       Deal with anything handleable at point before re-throwing 
       the query
    */
    throw e;
  }
}

and then call it three times, though doubtless in RealLife(TM) it would 
need to be a bit more complex because your example code doesn't show what 
is done with the query's results. Similarly, the method's catch block may 
be unnecessary if it cant add value by by being there.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Eclipse doesn't like else Eric Douglas <e.d.programmer@gmail.com> - 2017-05-12 10:44 -0700
  Re: Eclipse doesn't like else Marcel Mueller <news.5.maazl@spamgourmet.org> - 2017-05-12 20:30 +0200
    Re: Eclipse doesn't like else Eric Douglas <e.d.programmer@gmail.com> - 2017-05-12 11:53 -0700
      Re: Eclipse doesn't like else Arne Vajhøj <arne@vajhoej.dk> - 2017-05-13 20:39 -0400
        Re: Eclipse doesn't like else v_borchert@despammed.com (Volker Borchert) - 2017-05-14 03:50 +0000
  Re: Eclipse doesn't like else v_borchert@despammed.com (Volker Borchert) - 2017-05-12 18:15 +0000
    Re: Eclipse doesn't like else Eric Douglas <e.d.programmer@gmail.com> - 2017-05-12 13:33 -0700
  Re: Eclipse doesn't like else Martin Gregorie <martin@address-in-sig.invalid> - 2017-05-12 21:22 +0000
  Re: Eclipse doesn't like else v_borchert@despammed.com (Volker Borchert) - 2017-05-13 00:52 +0000
  Re: Eclipse doesn't like else Arne Vajhøj <arne@vajhoej.dk> - 2017-05-13 21:04 -0400

csiph-web