【环境:参考:在Eclipse中测试MySQL-JDBC(1)入门【数据库查询】】
【注意:】
本java代码中的知识修改了前面的【增加】的一句代码
st.executeUpdate("insert into employee values(null,'等等',30)");
修改为:
int count = st.executeUpdate("delete from employee where id = 4");
//下句代码,判断删除操作是否执行,并且提示影响了几条数据(可以删除)
System.out.println("您成功删除了" + count + "条记录");
java代码如下:
package com.flying.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class Demo {
@Test
public void shanchu() {
Connection ct =null;
Statement st =null;
try {
Class.forName("com.mysql.jdbc.Driver");
ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcDemo?characterEncoding=utf8", "root", "root");
st = ct.createStatement();
int count = st.executeUpdate("delete from employee where id = 4");
//下句代码,判断删除操作是否执行,并且提示影响了几条数据(可以删除)
System.out.println("您成功删除了" + count + "条记录");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (ct != null) {
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}