Results 1 to 5 of 5
  1. #1
    abhiN is offline Member
    Join Date
    Jan 2008
    Posts
    23
    Rep Power
    0

    Default image insertion into database

    hi

    can anybody send me sample progrm in java for how to insert and retrive image to and from mysql database..

    thanks..
    byee

  2. #2
    JForum4Hari is offline Member
    Join Date
    Jun 2008
    Posts
    5
    Rep Power
    0

    Default

    Use BLOB datatype and in Db you can use Image Datatype

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes Binary Large Object is the solution.

    Java Code:
    Statement stm; // You have to implement the statement to your connection
    ResultSet rs = stm.executeQuery(sql_query);
    
    if(rs.next()) {
        Blob blob = rs.getBlob(column);
    }

  4. #4
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    Hey I have done this before, so I dug this out of one of the classes I wrote.

    Apologies if its a little long winded or whatever, I wrote it over 2 years ago, but it is working within a production environment.

    You might need to play around with it a little bit but it should be enough:

    Java Code:
    //Writes filedata to database and returns the ID of the new Signature record
       private int writeImageFile(byte[] fileData, String fileName, String imageType, String mode, Integer signatureIDIn, HttpServletRequest request) throws Exception {
            //If the previous code found a file that can be uploaded then
            //save it into the database via a pstmt
            String sql = ""; 
            UtilDBquery udbq = getUser(request).connectToDatabase();
            Connection con = null; 
            int signatureID = 0;
            PreparedStatement pstmt = null;
            
            try {
                
                udbq.setUsePreparedStatements(true);
                con = udbq.getPooledConnection();
                con.setAutoCommit(false);
                
                if((!mode.equals("U")) || (mode.equals("U") && signatureIDIn == 0)) {
                    sql = "SELECT SEQ_SIGNATURE_ID.nextval FROM DUAL";
                    
                    
                    pstmt = con.prepareStatement(sql);
                    ResultSet rs = pstmt.executeQuery();
                    
                    while(rs.next()) {
                       signatureID = rs.getInt(1);
                    }
                    
                    if (fileName != null && imageType != null) {
                        sql = "INSERT INTO T_SIGNATURE (SIGNATURE_ID, SIGNATURE) values (?,?)";
                        
                        InputStream is2 = new ByteArrayInputStream(fileData);
                        
                        pstmt = con.prepareStatement(sql);
                        
                        pstmt.setInt(1, signatureID);
                        
                        pstmt.setBinaryStream(2, is2, (int)(fileData.length));
                        
                        pstmt.executeUpdate();
                        pstmt.close();
                        con.commit();
                        con = null;
                    }    
                }
                
                if(mode.equals("U") && signatureIDIn != 0) {
                    signatureID = signatureIDIn.intValue();
                    
                    if (fileName != null && imageType != null) {
                        sql = "UPDATE T_SIGNATURE SET SIGNATURE = ? WHERE SIGNATURE_ID = ?";
                        
                        InputStream is2 = new ByteArrayInputStream(fileData);
                        
                        pstmt = con.prepareStatement(sql);
                        
                        pstmt.setBinaryStream(1, is2, (int)(fileData.length));
                        pstmt.setInt(2, signatureID);
                        pstmt.executeUpdate();
                        pstmt.close();
                        con.commit();
                        con = null;
                    }
                }
                
                
            } catch (Exception e) {
                con = null;
                throw new Exception(e.toString());
                
            } 
            return signatureID;
       }

  5. #5
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    hi how can i insert or upload a picture to database using java(hiberante).

Similar Threads

  1. Replies: 0
    Last Post: 02-10-2008, 11:25 AM
  2. Retriving image from database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:41 PM
  3. passing image file to database
    By abhiN in forum Advanced Java
    Replies: 2
    Last Post: 01-21-2008, 06:30 AM
  4. database insertion
    By abhiN in forum New To Java
    Replies: 0
    Last Post: 01-17-2008, 07:24 AM
  5. Replies: 0
    Last Post: 08-15-2007, 01:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •