JDBC练习二
import java.sql.*;
public class JDBCTest02 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
String url = "jdbc:mysql://127.0.0.1:3306/mydb";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
int count = stmt.executeUpdate("delete from student where studentid = 5");
System.out.println(count == 1? "删除成功":"删除失败");
} catch(SQLException e){
e.printStackTrace();
} finally {
if(conn != null) {
try {
conn.close();
} catch(SQLException e){
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
} catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
运行结果:
