Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarrodhroberson/83d6b7a5549c9839ad1a030e694d0ce7 to your computer and use it in GitHub Desktop.
Save jarrodhroberson/83d6b7a5549c9839ad1a030e694d0ce7 to your computer and use it in GitHub Desktop.
import java.sql.*;
public class CloseAllResourcesWhenYouAreDonePreJava7
{
public static void main(final String[] args)
{
try
{
final Connection cn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
try
{
final Statement ps = cn.createStatement();
try
{
final ResultSet rs = ps.executeQuery("SELECT STATEMENT GOES HERE");
try
{
if (rs.next())
{
/* process the ResultSet */
}
else
{
throw new SQLException("no rows returned for \"SELECT MAX(msg_id) FROM msgs\"");
}
}
catch (final SQLException e)
{
throw new RuntimeException(e);
}
finally
{
try { rs.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); }
}
}
catch (final SQLException e)
{
throw new RuntimeException(e);
}
finally
{
try { ps.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); }
}
}
catch (final SQLException e)
{
throw new RuntimeException(e);
}
finally
{
try { cn.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); }
}
}
catch (final SQLException e)
{
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment