Skip to Main Content

Java and JavaScript in the Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

User_X1VCQOct 23 2021

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.

Comments

dvohra21

To store images in a database, convert the image to base64 encoding before storing.

1 - 1

Post Details

Added on Oct 23 2021
1 comment
695 views