Java(21):Java代码对Mysql数据库插入二进制数据

1、在数据库下新增表,表名:testblob CREATE TABLE `testblob` ( `id` int(19) NOT NULL AUTO_INCREMENT, `name1` blob NULL, `name2` tinyblob NULL, `name3` longblob NULL, `name4` blob NULL, `name5` tinyblob NULL, `name6` longbl...
摘要由CSDN通过智能技术生成

需求:需要往mysql数据库中同时给6个字段插入二进制数据

步骤:

1、在数据库下新增表,表名:testblob

CREATE TABLE `testblob`  (

  `id` int(19) NOT NULL AUTO_INCREMENT,

  `name1` blob NULL,

  `name2` tinyblob NULL,

  `name3` longblob NULL,

  `name4` blob NULL,

  `name5` tinyblob NULL,

  `name6` longblob NULL,

  PRIMARY KEY (`id`) USING BTREE

)

2.引用MySQL驱动jar包

进入maven目录

mvn install:install-file -Dfile=./newJar/mysql-connector-java-5.1.48.jar -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=5.1.48 -Dpackaging=jar

在项目中引用mysql:

    <dependencies>

        <!-- mysql依赖-->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-jav

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java 中,可以使用 JDBC API 来将从 MySQL 数据库中检索到的 BLOB 数据转换为图片并显示出来。下面是具体的步骤: 1. 使用 JDBC API 连接到 MySQL 数据库,并执行 SELECT 语句以检索包含图片的 BLOB 列。 ```java Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); PreparedStatement stmt = conn.prepareStatement("SELECT data FROM images WHERE id = ?"); stmt.setInt(1, 1); ResultSet rs = stmt.executeQuery(); ``` 其中,1 是图片所在行的 ID。 2. 从 ResultSet 对象中提取 BLOB 列中的二进制数据。 ```java if (rs.next()) { Blob blob = rs.getBlob("data"); InputStream inputStream = blob.getBinaryStream(); } ``` 3. 将二进制数据转换为图片,并将其显示在 UI 上。 ```java Image image = ImageIO.read(inputStream); JLabel label = new JLabel(new ImageIcon(image)); JFrame frame = new JFrame(); frame.getContentPane().add(label, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); ``` 其中,ImageIO 类用于将二进制数据转换为 Image 对象,JLabel 用于显示 Image 对象,JFrame 用于创建 UI 窗口。 完整的代码示例如下: ```java import java.awt.BorderLayout; import java.awt.Image; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class App { public static void main(String[] args) { try { Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); PreparedStatement stmt = conn.prepareStatement("SELECT data FROM images WHERE id = ?"); stmt.setInt(1, 1); ResultSet rs = stmt.executeQuery(); if (rs.next()) { Blob blob = rs.getBlob("data"); InputStream inputStream = blob.getBinaryStream(); Image image = ImageIO.read(inputStream); JLabel label = new JLabel(new ImageIcon(image)); JFrame frame = new JFrame(); frame.getContentPane().add(label, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } } catch (Exception e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值