Javaでtruncate文
Javaでtruncate文を書く方法です。そのままですが、java.sql.Statementを使用します。
以下、すごく簡単に書きますがコネクション取れる前提です。

import java.sql.*;

public class oracleDbConnect {
    public static void main(String[] args){

        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        
        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@
                  10.92.1.1:1521:instance","usr","pass");
            stmt = con.createStatement();
            rs = stmt.executeUpdate("TRUNCATE TABLE TBL_TEST");

        }catch(SQLException e){
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(rs != null){
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(stmt != null){
                    stmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(con != null){
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Back to top

Information