e260. getting and inserting binary data into an database table.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 28 行

TXT
28
字号
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) {
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?