WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java JDBC · Basic · question 18 of 100

How do you handle BLOB and CLOB data types using JDBC?

📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

BLOB and CLOB are data types used in databases to store large binary and character data respectively. In JDBC, BLOB and CLOB data types can be handled through the use of the java.sql.Blob and java.sql.Clob interfaces respectively.

Here are the steps to handle BLOB and CLOB data types using JDBC:

1. Establish a database connection: Before handling BLOB or CLOB data types, a connection to the database must first be established using the DriverManager class or DataSource object.

2. Prepare the SQL statement: Use the PreparedStatement object to create an SQL statement that contains the BLOB or CLOB data type. Examples of SQL statements include INSERT, UPDATE, and SELECT statements.

3. Convert the data to the appropriate data type: There are several ways to convert binary or character data to the corresponding BLOB or CLOB data type. One way is to use the setBinaryStream or setCharacterStream method of the PreparedStatement object, which takes an InputStream or Reader object respectively. For example:

    PreparedStatement ps = conn.prepareStatement("INSERT INTO my_table (my_blob) VALUES (?)");
    File file = new File("path/to/file.bin");
    FileInputStream fis = new FileInputStream(file);
    ps.setBinaryStream(1, fis, file.length());

4. Execute the SQL statement: Use the executeUpdate or executeQuery method of the PreparedStatement object to execute the SQL statement. The executeUpdate method is used for INSERT, UPDATE, and DELETE statements, while the executeQuery method is used for SELECT statements.

5. Retrieve the BLOB or CLOB data: Use the ResultSet object to retrieve BLOB or CLOB data from the database. Use the getBlob or getClob method of the ResultSet object to retrieve the BLOB or CLOB object respectively. For example:

    PreparedStatement ps = conn.prepareStatement("SELECT my_blob FROM my_table WHERE id=?");
    ps.setInt(1, 1);
    ResultSet rs = ps.executeQuery();
    if (rs.next()) {
        Blob blob = rs.getBlob("my_blob");
        InputStream is = blob.getBinaryStream();
        // do something with the input stream
    }

In summary, handling BLOB and CLOB data types using JDBC involves establishing a database connection, preparing an SQL statement that contains the BLOB or CLOB data type, converting the data to the appropriate data type, executing the SQL statement, and retrieving the BLOB or CLOB data from the ResultSet object.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java JDBC interview — then scores it.
📞 Practice Java JDBC — free 15 min
📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

All 100 Java JDBC questions · All topics