0

i'm trying to add image to database using netbeans but it's doesn't work for me this is my code when i go to mysql i see the image there but just octet size (BLOB - 50 o)

    String code=jTextField1.getText();
    String reference=jTextField2.getText();
    String nom=jTextField3.getText();
    String marque=jTextField4.getText();
    String dimention=jTextField5.getText();
    String quantite=jTextField6.getText();
    String discription=jTextPane1.getText();
    String famille =jTextField7.getText();
    String code_famille =jTextField10.getText();
    String sousfamille=jTextField8.getText();

    String requete="insert into piece (Code_Piece,Reference,Nom_P,Mark_P,Dimention,Quantite,Categorie,Type,Discription,Image) VALUES('"+
    code+"','"+reference+"','"+nom+"','"+marque+"','"+dimention+"','"+quantite+"','"+Cate+"','"+Type+"','"+discription+"','"+imgPath+"')";
SantiBailors
  • 1,596
  • 3
  • 21
  • 44
Yaya38
  • 3
  • 4
  • Looks like you are passing the path to the image on the file system by the variable name - not the actual byte data of the image? – user681574 Mar 18 '17 at 17:06
  • i think yes, but how can i store the byte of the image – Yaya38 Mar 18 '17 at 17:58
  • Check out this article. It loads the image file into memory and adds it to a PreparedStatement object. http://stackoverflow.com/questions/9430008/inserting-blob-data-in-java-using-preparedstatement – user681574 Mar 18 '17 at 18:14
  • thank you, but it doesn't work – Yaya38 Mar 18 '17 at 18:38
  • i mean this methode (INSERT INTO piece (image) VALUES (?)) it doesn't work i need like this insert into piece (Code_Piece,Reference,Nom_P,Mark_P,Dimention,Quantite,Categorie,Type,Discription,Image) VALUES('"+ code+"','"+reference+"','"+nom+"','"+marque+"','"+dimention+"','"+quantite+"','"+Cate+"','"+Type+"','"+discription+"','"+imgPath+"') – Yaya38 Mar 18 '17 at 18:51
  • and i wand to know what "'+imagePath+"' would be a string or byte or what ??? – Yaya38 Mar 18 '17 at 18:53

3 Answers3

0

i have this code inside my class too

JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new 
FileNameExtensionFilter("*.images","jpeg","jpg","png","bmp");
 file. addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
 if (result == JFileChooser.APPROVE_OPTION)
 {
 File selectedFile = file.getSelectedFile();
 String path = selectedFile.getAbsolutePath();
 lbl_image.setIcon(ResizeImage(path, null));
 imgPath = path;
 }

else if (result == JFileChooser.CANCEL_OPTION){
 System.out.println("No Fille Selected");
 }
Yaya38
  • 3
  • 4
0

this methode it doesn't work for me i don't know why ??

   InputStream img = new FileInputStream(new File(imgPath)) ;

       String sql="insert into 'stock'.'piece' (Code_Piece,Reference,Nom_P,Mark_P,Dimention,Quantite,Categorie,Type,Discription,Image) VALUES(jTextField1.getText(),'"+jTextField2.getText()+"','"+jTextField3.getText()+"','"+jTextField4.getText()+"','"+jTextField5.getText()+"','"+jTextField6.getText()+"','"+jTextField9.getText()+"','"+jTextField11.getText()+"','"+jTextPane1.getText()+"')";
       ps =conn.prepareStatement(sql);
        ps.setString(1, jTextField1.getText());
        ps.setString(2, jTextField2.getText());
        ps.setString(3, jTextField3.getText());
        ps.setString(4, jTextField4.getText());
        ps.setString(5, jTextField5.getText());
        ps.setString(6, jTextField6.getText());
        ps.setString(7, jTextField9.getText());
        ps.setString(8, jTextField11.getText());
        ps.setString(9,jTextPane1.getText());
        ps.setBlob(10, is);
        ps.execute();
Yaya38
  • 3
  • 4
0

The right way to use a BLOB would be to pass the InputStream itself. You can use the FileInputStream you are using to read the file.

File image = new File(path);
FileInputStream fis = new FileInputStream ( image );
String sql="insert into imgtst (username,image) values (?, ?)";
pst=con.prepareStatement(sql);
pst.setString(1, user);
pst.setBinaryStream (2, fis, (int) file.length() );

I got this from comment: https://stackoverflow.com/a/15036043/7808973; you should see it

Community
  • 1
  • 1