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:

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Python Comments Multiple Lines - Python
- How to send WhatsApp message from your Android App using Intent - WhatsApp
- Change TextEdit File Encoding - MacOS
- Program 13: Reverse a String - 1000+ Python Programs - Python-Programs
- How to delete a Page in Microsoft Word File on Mac (macOS) - MacOS
- Obsolete marquee element alternatives html5 - Html
- How to rename a Pandas DataFrame Column Names - Python
- Fix - Xode - Internal logic error: Connection was invalidated - MacOS