日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

05-JDBC连接MySQL数据库【删除数据】

發布時間:2025/3/20 数据库 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 05-JDBC连接MySQL数据库【删除数据】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JDBC自學教程–終篇總結:

地址:http://blog.csdn.net/baidu_37107022/article/details/72600018

1.實現修改步驟

前三個步驟:注冊、獲得連接,創建statement對象方法,見上一節:

02-JDBC實戰–JDBC查詢數據庫MySQL–http://blog.csdn.net/baidu_37107022/article/details/72597975

2.使用jdbc刪除數據庫中的數據

這里使用的是queryDemo數據庫,表格為demo1student,表中數據如下:

1)刪除單個數據

代碼演示

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties;import org.junit.Test;public class Test11 {// 刪除數據//刪除單個數據 @Testpublic void deleteOne() {Connection connection = null;PreparedStatement ps = null;try {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/queryDemo";Properties info = new Properties();info.put("user", "root");info.put("password", "123");connection = DriverManager.getConnection(url, info);String sql = "delete from demo1student where id between ? and ? ";ps = connection.prepareStatement(sql);ps.setInt(1, 13);ps.setInt(2, 17);int num = ps.executeUpdate();if (num > 0) {// 如果刪除成功,則打印successSystem.out.println("Sucess");} else {// 如果刪除失敗,則打印FailureSystem.out.println("Failure");}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {// 5.關閉資源if (connection != null) {try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (ps != null) {try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

運行結果:

1.刪除前

2.刪除后


2)刪除表格中所有數據

代碼演示

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties;import org.junit.Test;public class Test12 {// 刪除表格中所有數據@Testpublic void deleteAll() {Connection connection = null;PreparedStatement ps = null;try {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/queryDemo";Properties info = new Properties();info.put("user", "root");info.put("password", "123");connection = DriverManager.getConnection(url, info);String sql = "delete from demo1student";ps = connection.prepareStatement(sql);int num = ps.executeUpdate();if (num > 0) {// 如果刪除成功,則打印successSystem.out.println("Sucess");} else {// 如果刪除失敗,則打印FailureSystem.out.println("Failure");}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {// 5.關閉資源if (connection != null) {try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (ps != null) {try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }

運行結果:
1.刪除前

2.刪除后

總結

以上是生活随笔為你收集整理的05-JDBC连接MySQL数据库【删除数据】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。