JDBC练习----删除数据
例:Demo02数据库中删除一条数据
import java.sql.*;
public class JDBCDemo4 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///demo01", "root", "root");
String sql = "delete from demo02 where id = 1";
stmt = conn.createStatement();
int count = stmt.executeUpdate(sql);
System.out.println(count);
if (count > 0){
System.out.println("Delete Success");
}else{
System.out.println("Delete Failed");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}