I'm encountering errors when connecting to MySQL from Java, so I'm wrapping the connection establishment in a try statement. However, doing this means that any attempt to use the Connection variable afterwards throws a variable conn might not have been initialized error.
What is the proper way to do this?
What I have:
Connection conn;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "alexis","pass");
}
catch (SQLException e) {
System.err.println("SQL exception: " + e.getMessage());
System.exit(1);
}
if (!conn.isClosed()) {
conn.close();
}
Error:
> variable conn might not have been initialized