In this Tutorial, we will take a look at how to delete a database table record using Java JDBC PreparedStatement,
Table Entries before deletemysql> select * from my_table;
+----+-------+
| id | data |
+----+-------+
| 1 | Hello |
| 2 | Hi |
| 1 | Bye |
+----+-------+
3 rows in set (0.00 sec)
Code Example:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBCCreateTableExample {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url ="jdbc:mysql://localhost:3306/my_uat";
String userName="root";
String password ="root123";
String deleteQuery ="delete from my_table where id=?";
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery);
preparedStatement.setInt(1, 2);
int count = preparedStatement.executeUpdate();
if(count > 0) {
System.out.println("Record deleted from the table...");
} else {
System.out.println("Error occurred while detecting the record from the table..");
}
}
}
Output: Record deleted from the table...
The table now has only two entries:

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!