【问题标题】:How to add a photo to mysql from java?如何从java将照片添加到mysql?
【发布时间】:2015-06-05 19:33:49
【问题描述】:

我使用 netbeans 创建了一个软件。现在我想将图片添加到我的数据库中。我创建了一个表并将类型更改为“BLOB”。但是,IDK 如何在 java 中编写代码来做到这一点。 有了这个,我得到一个图像并将其设置为 jLabel。 现在如何将这张照片保存在 mysql 中?

try {
            lbl_imge1.setIcon(null);
            jFileChooser1.showOpenDialog(this);
            BufferedImage upload = ImageIO.read(jFileChooser1.getSelectedFile());
            java.awt.Image photo = upload.getScaledInstance(lbl_imge1.getWidth(), lbl_imge1.getHeight(), java.awt.Image.SCALE_SMOOTH);
            lbl_imge1.setIcon(new ImageIcon(photo));
        } catch (Exception e) {
            e.printStackTrace();
        }

现在我来了,

try {
            jLabel1.setIcon(null);
            jFileChooser1.showOpenDialog(this);
            BufferedImage upload = ImageIO.read(jFileChooser1.getSelectedFile());
            java.awt.Image photo = upload.getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(), java.awt.Image.SCALE_SMOOTH);
            jLabel1.setIcon(new ImageIcon(photo));

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crazy", "root", "123");

            BufferedImage buffered = ImageIO.read(jFileChooser1.getSelectedFile());

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(buffered, "jpg", baos);
            byte[] imageInByte = baos.toByteArray();

            Blob blob = con.createBlob();
            blob.setBytes(1, imageInByte);

            String query="INSERT INTO image VALUES ('"+jTextField1.getText()+"','"+blob+"')";
            PreparedStatement statement=con.prepareStatement(query);

【问题讨论】:

    标签: java mysql blob


    【解决方案1】:
    1. 拿你的 BufferedImage

      BufferedImage buffered= ImageIO.read(jFileChooser1.getSelectedFile());
      
    2. 从中获取一个字节数组 (From this answer)

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(buffered, "jpg", baos );
      byte[] imageInByte = baos.toByteArray();
      
    3. 将字节数组保存为 blob (From this answer)(可能使用 Prepared Statement)

      Blob blob = connection.createBlob();
      blob.setBytes(1, imageInByte);
      

    更新:连接是您的数据库连接器,即:

    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
    

    【讨论】:

    • 你能描述一下第三步吗?什么是“连接”? netbeans 给了我错误。
    • 是你的数据库连接! IE:Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");我更新了答案;)
    • 这取决于您如何管理实体和数据库的关系...您有Hibernate MappingsEntityManager 吗?也许一个简单的HQL 查询对您来说就足够了,像在其他情况下那样做...如果没有,PreparedStatement 按照答案中的建议...String query = "insert into table(......)"; PreparedStatement ps=connection.prepareStatement(query);
    • 声明语句=con.createStatement(); statement.executeUpdate("插入图像值 ('"+jTextField1.getText()+"','"+blob+"')");这节省了一个内存地址。那样可以么?如果是,如何将其作为图像恢复?
    • 现在我用 PreparedStatement 尝试了相同的查询。但什么也没发生。没有任何东西保存在数据库中。
    【解决方案2】:

    您可以将任何字节数组保存到 blob 列。我知道这对于 Oracle 是可能的,这也应该适用于 MySql。
    下面是一个如何使用 java 代码插入的小例子:

    String sql = "insert into yourtable(id,binaryImage) values (?,?)";
    PreparedStatement pstmt =dbConnection.prepareStatement(sql);
    
    ByteArrayInputStream bais = new ByteArrayInputStream(your_byte_array);
    pstmt.setString(1, "your_id");
    pstmt.setBinaryStream(2, bais);
    pstmt.execute();
    pstmt.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      相关资源
      最近更新 更多