Forum Stats

  • 3,824,920 Users
  • 2,260,440 Discussions
  • 7,896,347 Comments

Discussions

How to insert a BLOB into my MySQL data file in JAVA15.0.1?

Dear Madam/Sir,

  How are you? I do hope you are well and had a good day.

I got an error on my Java15.0.1 program. I’m trying to insert into my MySQL data file, a BLOB field.I’m using Xampp connector, thus mariadb. The BLOB is an png file.


CODE:

//By: Ishraga Mustafa Awad Allam. On 22 - 10 - 2021.


import java.sql.*;

import java.io.BufferedReader;

import java.io.*;

import java.io.FileNotFoundException;

import java.nio.file.Files;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;


public class RowInsertBlob2 {


   private static final String SQL_INSERT = "INSERT INTO tbl (id, NAME, ADDRESS, MyBLOB) VALUES (?, ?, ?, ?)";

              

   public static void main(String[] args) {

               String cd;

       try (Connection conn = DriverManager.getConnection(

               "jdbc:mariadb://127.0.0.1:3306/python","root", "");

            PreparedStatement preparedStatement = conn.prepareStatement(SQL_INSERT)) {

                                   

                                   BufferedReader my_Reader = new BufferedReader(new FileReader(new File(

                                                           "C://code//javacode//bin//Arabing.txt")));

                                   String line = "";

                                   String[] rawString = {"", "", ""};

                      int i = 0;

                                   while((line = my_Reader.readLine()) != null)

                                   {

                                               rawString[i] = line;

                                               i = i + 1;

                                   }

                                   

                                   String str = "الثورة الحارة الساسة";

           File f = new File("c://code//python//CNN.png", "rb");

                                   FileInputStream fr = new FileInputStream(f);

                                   long fsize = f.length();

                                   

                                   for(int j = 0; j < 3; j++){           

                                    java.sql.Statement stat = conn.createStatement();

                                    preparedStatement.setInt(1, j + 80);

                                   preparedStatement.setString(2, rawString[j]);

                                    preparedStatement.setString(3, str);

                                    PreparedStatement.setBlob(4, f, f.available()); // Here is the ERROR

                       

                                   

                                    int row = preparedStatement.executeUpdate();


           // rows affected

             System.out.println(row); //1

                                   }

       my_Reader.close();

                       fr.close();

       } catch (SQLException e) {

           System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());

       } catch (Exception e) {

           e.printStackTrace();

       }

   }

}


Please help me with my program. I did try the GOOGLE search, but I did not find a good example to follow.

Thank you.

Best Regards,

Ishraga from Sudan.

Answers