We can make use of Statement or PreparedStatement to truncate a database table using Java JDBC,
Table: student:mysql> select * from student;
+------------+--------------+
| student_id | student_name |
+------------+--------------+
| 1 | Sam |
| 2 | Andy |
+------------+--------------+
2 rows in set (0.00 sec)
Java Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcExampleTruncateTable {
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/my_uat";
String userName = "root";
String password = "root123";
String truncateTableQuery = "truncate table student";
Connection connection = null;
try {
connection = DriverManager.getConnection(url, userName, password);
Statement statement = connection.createStatement();
statement.executeUpdate(truncateTableQuery);
System.out.println("Student table truncated!...");
} catch (SQLException e) {
System.out.println("Exception Occurred while truncating the table!");
e.printStackTrace();
} finally {
connection.close();
}
}
}
Output:
Student table truncated!...

As you can see the table is now empty!
Have Questions? Post them here!
- Error: Can not find the tag library descriptor for
- Create a Database Table using JDBC PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- Java Jackson with Maven Example
- [fix] Java JDBC ConnectException: Connection refused
- Spring Boot: Transactions Management with JDBCTemplate Example
- Java Get Current Date for a Specific Time Zone
- What are the 8 Primitive Data Types in Java
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Maven Eclipse (M2e) No archetypes currently available
- How to Sort a LinkedList in Java
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Split a String in Java with Examples
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Truncate table using Java JDBC Example
- Java: Generate random numbers within a range
- Parse XML file in Java using DOM Parser
- How to get Client IP address using Java Code Example
- JDBCTemplate Querying Examples with Spring Boot 3
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- String Boot + Redis - SET and GET String Commands Examples
- Setting up Spring Boot 3 + Maven + MySQL + JDBC Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- Java Split String by Spaces
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Understanding AWS Calculator: DynamoDB on-demand capacity - AWS
- Rename git branch on Local and GitHub Remove using Command - Git
- How to open a new tab in Notepad++ - NotepadPlusPlus
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds - Java
- Spotlight Search Mac Keyboard Shortcut - MacOS
- Fix: Deprecated Gradle features were used in this build, making it incompatible with Gradle - Gradle
- Minecraft Java Edition - Java
- SharePoint An unexpected error has occurred - Correlation ID and PowerShell Merge-SPlogfile - SharePoint