Connection
Statement
DriverManager
public class Demo02 {
// JDBC執(zhí)行刪除
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 注冊驅(qū)動
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
// 獲取連接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lianxi", "root", "******");
stmt = conn.createStatement();
// 執(zhí)行SQL語句
String sql = "delete from demo1 where age = 18";
// 處理查詢結(jié)果集
int c = stmt.executeUpdate(sql);
System.out.println(c == 1 ? "刪除成功" : "刪除失敗");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 釋放資源
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}