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,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later. - Android
- Installing Visual Studio Code on Raspberry Pi - HowTos
- Android : No Launcher activity found! Error - Android
- How to Sort a LinkedList in Java - Java
- How to Install CVS Version Control on Linux/Ubuntu - Linux
- How to test Exceptions using Java JUnit - Java
- Android Studio Error:(19, 0) Gradle DSL method not found: android() - Android-Studio
- macOS Mail Fix: Authorization Error 400: invalid request - MacOS