The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.sql  [73 examples] > Inserting and Updating Data  [4 examples]

e260. Getting and Inserting Binary Data into an Database Table

This 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) {
    }

 Related Examples
e258. Inserting a Row into a Database Table
e259. Inserting a Row into a Database Table Using a Prepared Statement
e261. Updating a Row in a Database Table

See also: Batching    Connections    Database Meta Data    Deleting Data    Drivers    Importing and Exporting    Oracle OBJECTs    Oracle VARRAYs    Procedures and Functions    Retrieving Data    Scrollable Result Sets    Tables    Updatable Result Sets   


© 2002 Addison-Wesley.