mitchyb mitchyb - 2 months ago 5x
Java Question

Explanation to why connection = null in JDBC

Every time I check out a tutorial for JDBC programmers seem to set the connection as null

Connection conn = null;

and I can't figure out why. Can someone clarify this for me?


Usually, it's so the finally block can include a check like

} finally {
    // ...
    if (conn != null) {
        try {
        } catch (Exception e) {

Without the default initialization to null, you'd get an error that conn might not have been initialized.