[JDBC] 출력 스트림 사용하기

setAsciiStream()을 사용하여 ASCII 값을 데이터베이스에 저장

import java.io.ByteArrayInputStream;
import java.sql.*;

public class TestApplication {

    static final String DB_URL = "jdbc:mysql://localhost/YOUR_DATABASE";
    static final String USER = "yourUsername";
    static final String PASS = "yourPassword";
    static final String XML = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";

    static final String DROP_TABLE_SQL = "DROP TABLE IF EXISTS XML_Data";
    static final String CREATE_TABLE_SQL = "CREATE TABLE XML_Data (id INT PRIMARY KEY AUTO_INCREMENT, data CLOB)";
    static final String INSERT_SQL = "INSERT INTO XML_Data (data) VALUES (?)";
    static final String SELECT_SQL = "SELECT data FROM XML_Data";

    public static void main(String[] args) {
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             Statement stmt = conn.createStatement();
             PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL)) {

            createXMLTable(stmt);

            ByteArrayInputStream input = new ByteArrayInputStream(XML.getBytes());
            pstmt.setAsciiStream(1, input, XML.length());
            pstmt.execute();

            ResultSet rs = stmt.executeQuery(SELECT_SQL);
            while (rs.next()) {
                String xmlData = rs.getAsciiStream(1).toString();
                System.out.println("XML Data: " + xmlData);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void createXMLTable(Statement stmt) throws SQLException {
        stmt.execute(DROP_TABLE_SQL);
        stmt.execute(CREATE_TABLE_SQL);
    }
}

setBinaryStream()을 사용하여 바이너리 값을 데이터베이스에 저장

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BinaryStreamExample {
    static final String DB_URL = "jdbc:mysql://localhost/testdb";
    static final String USER = "root";
    static final String PASS = "password";
    static final String INSERT_IMAGE_SQL = "INSERT INTO Images (image) VALUES (?)";

    public static void main(String[] args) {
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(INSERT_IMAGE_SQL);
             FileInputStream fis = new FileInputStream("path/to/image.jpg")) {

            pstmt.setBinaryStream(1, fis, fis.available());
            pstmt.executeUpdate();
            System.out.println("Image inserted successfully.");
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }
}

setCharacterStream()을 사용하여 문자 스트림을 데이터베이스에 저장

import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CharacterStreamExample {
    static final String DB_URL = "jdbc:mysql://localhost/testdb";
    static final String USER = "root";
    static final String PASS = "password";
    static final String INSERT_TEXT_SQL = "INSERT INTO Texts (content) VALUES (?)";

    public static void main(String[] args) {
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(INSERT_TEXT_SQL);
             FileReader reader = new FileReader("path/to/large_text_file.txt")) {

            pstmt.setCharacterStream(1, reader);
            pstmt.executeUpdate();
            System.out.println("Text inserted successfully.");
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }
}