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:

-
Have Questions? Post them here!
More Posts related to Java,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- Java JDBC Delete a Record in Database Table using PreparedStatement - Java
- [fix] openssl No such file or directory error C++ - Ubuntu
- How to generate ssh key? - HowTos
- Top 10 emerging breakthrough trending technologies - HowTos
- macOS say command text to speech using various voices and languages - MacOS
- Opening mac Terminal - MacOS
- Add Line Number before each line in Notepad++ using Column Editor - NotepadPlusPlus
- Eclipse Error The JVM Shared Library JavaVirtualMachines does not contain the JNI_CreateJavaVM symbol - Eclipse