Java SQL Table truncateTable(Connection conn, String table_name)

Here you can find the source of truncateTable(Connection conn, String table_name)

Description

truncate Table

License

Apache License

Declaration

public static void truncateTable(Connection conn, String table_name) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.*;

public class Main {
    public static void truncateTable(Connection conn, String table_name) {
        Statement truncateStmt = null;
        Statement countStmt = null;
        String truncateSQL = "TRUNCATE TABLE " + table_name;
        String countSQL = "SELECT COUNT(*) as count FROM " + table_name;

        try {/* w w w.  j  a  va 2 s  .  c o  m*/
            truncateStmt = conn.createStatement();
            truncateStmt.executeUpdate(truncateSQL);

            countStmt = conn.createStatement();
            ResultSet rs = countStmt.executeQuery(countSQL);
            System.out.println("Truncated " + table_name);
            while (rs.next()) {
                System.out.println(rs.getInt("count") + " rows");
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

Related

  1. normalizeTableName(String table, Connection con)
  2. parse(Connection connection, String tableName, String type)
  3. queryAllFromTable(String tableName)
  4. quoteSchemaTable(Connection conn, String schema, String table)
  5. truncateTable(Connection conn)
  6. truncateTable(Connection conn, String tablename)