DeleteClobFromOracleServlet.java Source code

Java tutorial

Introduction

Here is the source code for DeleteClobFromOracleServlet.java

Source

/*
Defining the Table: Oracle 9i
    
The following defines a table based on Oracle 9i:
    
create table DataFiles (
id INT PRIMARY KEY,
fileName VARCHAR(20),
fileBody CLOB
);
    
Defining the Table: MySQL
    
The following defines a table based on MySQL:
    
create table DataFiles (
id INT PRIMARY KEY,
fileName VARCHAR(20),
fileBody TEXT
);
    
*/
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteClobFromOracleServlet extends HttpServlet {

    public static Connection getConnection() throws Exception {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
        String username = "userName";
        String password = "password";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        Connection conn = null;
        PreparedStatement pstmt = null;
        String id = "001";
        ServletOutputStream out = response.getOutputStream();
        response.setContentType("text/html");
        out.println("<html><head><title>Delete CLOB Record</title></head>");

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement("delete from DataFiles where id = ?");
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            out.println("<body><h4>deleted CLOB record with id=" + id + "</h4></body></html>");
        } catch (Exception e) {
            out.println("<body><h4>Error=" + e.getMessage() + "</h4></body></html>");
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doGet(request, response);
    }
}