Getting and Inserting Binary Data into an Database Table

This example inserts and retrieves binary data into the table created in Creating a MySQL Table to Store Java Types.
try { // Prepare a statement to insert binary data String sql = "INSERT INTO mysql_all_table (col_binarystream) VALUES(?)"; PreparedStatement pstmt = connection.prepareStatement(sql); // Create some binary data byte[] buffer = "some data".getBytes(); // Set value for the prepared statement pstmt.setBytes(1, buffer); // Insert the data pstmt.executeUpdate(); pstmt.close(); // Select records from the table Statement stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery("SELECT * FROM mysql_all_table"); while (resultSet.next()) { // Get data from the binary column byte[] bytes = resultSet.getBytes("col_binarystream"); } } catch (SQLException e) { }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.