Created
July 13, 2021 14:29
-
-
Save pablohdzvizcarra/a67ad0ab50438e2fdd1fb65201c19e76 to your computer and use it in GitHub Desktop.
operations to save and read clobs, blobs in mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pluralsight.jdbc.course.m6c1; | |
| import java.io.FileInputStream; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.io.Reader; | |
| import java.sql.*; | |
| public class SavesAndReadBlobsAndClobs | |
| { | |
| private final String URL | |
| = "jdbc:mysql://localhost:3306/classicmodels?user=root&password=my-secret-pw"; | |
| /** | |
| * Stores a text file in a row in a specific column in the database | |
| * | |
| * @param columnName The name of the column to which we will assign the data | |
| * @param inStream One {@link InputStreamReader} with the data to | |
| * @return True if ok | |
| * @throws SQLException Any exception occurred | |
| */ | |
| public boolean storeClob(String columnName, InputStreamReader inStream) throws SQLException | |
| { | |
| String query = "UPDATE productlines SET htmlDescription=? WHERE productLine=?"; | |
| try(Connection connection = DriverManager.getConnection(URL); | |
| PreparedStatement preparedStatement = connection.prepareStatement(query)) | |
| { | |
| preparedStatement.setString(2, columnName); | |
| preparedStatement.setCharacterStream(1, inStream); | |
| preparedStatement.executeUpdate(); | |
| return true; | |
| } | |
| } | |
| public Reader readClob(String productLine) throws SQLException | |
| { | |
| String query = "SELECT htmlDescription FROM productlines WHERE productLine=?"; | |
| try(Connection connection = DriverManager.getConnection(URL); | |
| PreparedStatement preparedStatement = connection.prepareStatement(query)) | |
| { | |
| preparedStatement.setString(1, productLine); | |
| try(ResultSet resultSet = preparedStatement.executeQuery()) | |
| { | |
| if (resultSet.next()) | |
| { | |
| return resultSet.getCharacterStream(1); | |
| } else | |
| { | |
| return null; | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * Store a file in a specified row in a table in the database | |
| * | |
| * @param columnName The name of the column to which the data will be assigned | |
| * @param file A {@link FileInputStream} with the data | |
| * @return True if ok | |
| * @throws SQLException Any occurred exception | |
| */ | |
| public boolean storeBlob(String columnName, FileInputStream file) throws SQLException | |
| { | |
| String query = "UPDATE productlines SET image=? WHERE productLine=?"; | |
| try(Connection connection = DriverManager.getConnection(URL); | |
| PreparedStatement preparedStatement = connection.prepareStatement(query)) | |
| { | |
| preparedStatement.setBinaryStream(1, file); | |
| preparedStatement.setString(2, columnName); | |
| int result = preparedStatement.executeUpdate(); | |
| return result > 0; | |
| } | |
| } | |
| public InputStream readBlob(String columnName) throws SQLException | |
| { | |
| String query = "SELECT image FROM productlines WHERE productLine=?"; | |
| try(Connection connection = DriverManager.getConnection(URL); | |
| PreparedStatement statement = connection.prepareStatement(query)) | |
| { | |
| statement.setString(1, columnName); | |
| try(ResultSet resultSet = statement.executeQuery();) | |
| { | |
| if (resultSet.next()) | |
| { | |
| return resultSet.getBinaryStream(1); | |
| } | |
| } | |
| } | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment