package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入你要删除的部门id");
int id = input.nextInt();
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.创建连接
Connection conn = null;
Statement stmt = null;
try {
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/test_2", "root","root");
//3.获取Statement对象
stmt = conn.createStatement();
//4.通过stmt对象实现对数据库的增删改
//注意:最好的建议是,在执行java操作数据库语句的时候
//自己先在数据库中模拟执行以下类似的语句看看是否能成功...
String sql = "delete from dept where id="+id;
int n = stmt.executeUpdate(sql);
if(n >= 1){
System.out.println("删除数据成功");
}else{
System.out.println("删除数据失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入你要删除的部门id");
int id = input.nextInt();
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.创建连接
Connection conn = null;
Statement stmt = null;
try {
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/test_2", "root","root");
//3.获取Statement对象
stmt = conn.createStatement();
//4.通过stmt对象实现对数据库的增删改
//注意:最好的建议是,在执行java操作数据库语句的时候
//自己先在数据库中模拟执行以下类似的语句看看是否能成功...
String sql = "delete from dept where id="+id;
int n = stmt.executeUpdate(sql);
if(n >= 1){
System.out.println("删除数据成功");
}else{
System.out.println("删除数据失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}