![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e260. Getting and Inserting Binary Data into an Database TableThis example inserts and retrieves binary data into the table created in e248 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) { }
e259. Inserting a Row into a Database Table Using a Prepared Statement e261. Updating a Row in a Database Table © 2002 Addison-Wesley. |