package connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbctest2 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//1.注册驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zwj", "root", "123");
//3.获取数据库操作对象
stmt = conn.createStatement();
//4.执行sql语句
// String sql = "delete from zwj where id=1";
// jdbc中,sql语句不需要以分号结尾
String sql="update zwj set stu_name='小雯' where id=4";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "删除成功" : "删除失败");
} catch (SQLException e) {
e.printStackTrace();
} finally {
//5.释放资源
if (stmt != null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
JDBC使用数据库更新以及删除
最新推荐文章于 2024-05-11 15:30:00 发布