Results 1 to 2 of 2

Thread: Uploading and Retrieving images using JAVA/JDBC

  1. #1
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Uploading and Retrieving images using JAVA/JDBC

    I am writing a swing based Java program and need to upload a photo into mySQL DB and retrieve it and show it on the desktop application. Its a simple application and does not use any JSP servlet libraries. I tried using FileInputStream and ByteArry inputStream but they do not seem to work.

    Please help!!


  2. #2
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: Uploading and Retrieving images using JAVA/JDBC

    Here is the sample program....

    Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    import java.sql.Blob;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.*;
    
    class MySqlInsertBlob { 
    FileOutputStream image;
    Connection conn = null; 
    PreparedStatement pstmt = null; 
    Statement stmt= null; 
    ResultSet res = null; 
    StringBuffer query=null; 
    String filename ="c:/backblue.gif"; 
    
    public MySqlInsertBlob(){ 
    
    try{ 
    
    query=new StringBuffer("insert into blobs(filename,binarydata,name) values (?,?,?)"); 
    File file= new File(filename); 
    
    Class.forName("com.mysql.jdbc.Driver"); 
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","raghu123"); 
    stmt=conn.createStatement();
    pstmt=conn.prepareStatement(query.toString()); 
    pstmt.setString(1, filename); 
    pstmt.setBinaryStream(2, new FileInputStream(filename),(int)file.length()); 
    pstmt.setString(3,"silviya"); 
    pstmt.executeUpdate(); 
    System.out.println("Successfully inserted into BLOBS ....."); 
    
    ResultSet rs=stmt.executeQuery("select * from blobs where name='silviya'");
    if (rs.next()) {
    Blob test=rs.getBlob("binarydata");
    InputStream x=test.getBinaryStream();
    int size=x.available();
    OutputStream out=new FileOutputStream("c:/xyz.gif");
    byte b[]= new byte[size];
    x.read(b);
    out.write(b);
    }
    }catch(ClassNotFoundException cnfe){ 
    
    System.out.println("ClassNotFoundException occured :"+cnfe); 
    }catch(SQLException se){ 
    
    System.out.println("SQLException occured :"+se); 
    }catch(FileNotFoundException fe){ 
    
    System.out.println("File Not Found Exception occured :"+fe); 
    }catch(Exception e){ 
    
    System.out.println("Exception occured :"+e); 
    }finally{ 
    try{ 
    stmt.close(); 
    conn.close(); 
    }catch(Exception e){} 
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact