~/ learn/ java-from-zero/ cards/ Resource management & transactions
1 of 4

Type a try-with-resources block opening Connection and PreparedStatement

Type a try-with-resources block opening Connection and PreparedStatement

Answer

try (Connection conn = DriverManager.getConnection(url, user, pass); PreparedStatement ps = conn.prepareStatement(sql)) { ps.executeUpdate(); }

Both Connection and PreparedStatement are AutoCloseable. try-with-resources closes ps first, then conn — reverse declaration order — even if an exception is thrown.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/bf4a596f-f0da-4a21-bcba-f2e1f61f1721/flashcard utf-8 LF