You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vertx 3.9.12版本JDBCClient如何向数据库BLOB字段插入图片

Hey there! Since you're working with Vert.x 3.9.12's JDBCClient, inserting an image into a BLOB field is totally straightforward. Let me break down the process for you:

1. Convert your image to a byte array

First, you need to get your image data into a byte array. You can use Vert.x's asynchronous FileSystem API to read local files, or handle input streams if the image comes from elsewhere. Here's an example using Vert.x's FileSystem:

vertx.fileSystem().readFile("/path/to/your/image.png", ar -> {
    if (ar.succeeded()) {
        Buffer imageBuffer = ar.result();
        byte[] imageBytes = imageBuffer.getBytes();
        // Proceed to insert the byte array into the database
        insertImageToBlob(imageBytes);
    } else {
        // Handle file read failure
        ar.cause().printStackTrace();
    }
});
2. Insert the byte array using JDBCClient

Vert.x's JDBCClient works seamlessly with prepared statements, and in 3.9.12, you can directly pass the byte array as a parameter—it will automatically map to the BLOB field. Here's how to do the insertion:

private void insertImageToBlob(byte[] imageBytes) {
    // Assume you've already initialized your JDBCClient instance as `jdbcClient`
    jdbcClient.getConnection(connAr -> {
        if (connAr.succeeded()) {
            SQLConnection connection = connAr.result();
            // Replace with your actual table and column names
            String insertQuery = "INSERT INTO image_table (image_blob) VALUES (?)";
            
            connection.updateWithParams(insertQuery, new JsonArray().add(imageBytes), updateAr -> {
                if (updateAr.succeeded()) {
                    UpdateResult result = updateAr.result();
                    System.out.println("Image inserted successfully! Rows affected: " + result.getUpdated());
                } else {
                    System.err.println("Failed to insert image: " + updateAr.cause().getMessage());
                }
                // Always close the connection after use
                connection.close(closeAr -> {
                    if (closeAr.failed()) {
                        closeAr.cause().printStackTrace();
                    }
                });
            });
        } else {
            System.err.println("Failed to get database connection: " + connAr.cause().getMessage());
        }
    });
}
Quick notes to keep in mind
  • Make sure your database column is of type BLOB (or a suitable large binary type like LONGBLOB for larger images in MySQL).
  • For extremely large images, you might want to look into streaming the data, but for most use cases, passing the byte array directly works perfectly.
  • Vert.x 3.9.12 handles the byte array to BLOB conversion automatically—no need to manually create a JDBC Blob object.

内容的提问来源于stack exchange,提问作者Phannin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 11:04:09