從Java連接到MySQL時遇到錯誤,因此我將連接建立包裝在try
語句中。但是,這樣做意味着任何嘗試使用Connection
變量之後都會拋出variable conn might not have been initialized
錯誤。 這樣做的正確方法是什麼? 我有什麼:Java:try-catch MySQL異常
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();
}
錯誤:
> variable conn might not have been initialized