要刪除Java中的數據庫數據,你可以使用Java的JDBC(Java數據庫連接)來連接和操作數據庫。下面是一個簡單的示例代碼來刪除數據庫中的數據:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class DeleteDataExample {????public?static?void?main(String[]?args)?{
????????Connection?connection?=?null;
????????PreparedStatement?preparedStatement?=?null;
????????try?{
????????????//?連接數據庫
????????????connection?=?DriverManager.getConnection(“jdbc:mysql://localhost:3306/db_name”,?"
????????????username",?“password”);
????????????//?創建刪除數據的SQL語句
????????????String?sql?=?“DELETE?FROM?table_name?WHERE?column_name?=??”;
????????????//?創建PreparedStatement對象,并設置參數
????????????preparedStatement?=?connection.prepareStatement(sql);
????????????preparedStatement.setString(1,?“value”);
????????????//?執行刪除操作
????????????int?rowsDeleted?=?preparedStatement.executeUpdate();
????????????//?輸出刪除的行數
????????????System.out.println(“Deleted?"?+?rowsDeleted?+?"?rows”);
????????}?catch?(SQLException?e)?{
????????????e.printStackTrace();
????????}?finally?{
????????????try?{
????????????????//?關閉連接和PreparedStatement對象
????????????????if?(preparedStatement?!=?null)?{
????????????????????preparedStatement.close();
????????????????}
????????????????if?(connection?!=?null)?{
????????????????????connection.close();
????????????????}
????????????}?catch?(SQLException?e)?{
????????????????e.printStackTrace();
????????????}
????????}
????} }
請根據你的實際情況修改連接字符串(URL)、數據庫名稱(db_name)、用戶名(username)、密碼(password)、表名稱(table_name)和列名稱(column_name)以及要刪除的值(value)。
這是一個基本的示例,你可以根據自己的需求進行調整。請確保已經正確導入JDBC驅動程序。