retrieve image from saved path java

Solutions on MaxInterview for retrieve image from saved path java by the best coders in the world

showing results for - "retrieve image from saved path java"
Matilda
09 Oct 2018
1package ImageTutorial;
2 
3import java.sql.*;
4import java.io.*;
5 
6public class RetrieveImage
7{
8    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
9    static final String dburl = "jdbc:mysql://localhost/STOREDB";    
10    static final String dbuser = "root";
11    static final String dbpass = "root";
12    
13public static void main(String[] args) throws Exception, IOException, SQLException
14{
15 Connection con = null;
16 FileOutputStream fs=null;
17 PreparedStatement ps=null;
18 
19 try 
20 {
21     //Step 1 : Connecting to server and database
22     con = DriverManager.getConnection(dburl, dbuser, dbpass);
23     ps= con.prepareStatement("SELECT * FROM ITEM WHERE SavePic IS NOT NULL");
24     ResultSet rset=ps.executeQuery();
25     	    
26     byte b[];
27     Blob blob;
28     int i=1;
29     while(rset.next())
30     {
31      i++;
32      System.out.print("ID: " + rset.getInt(1));
33      System.out.print(" Product : "+rset.getString(2));
34      System.out.println(" Price : "+rset.getString(3));
35      
36      File f=new File("/home/prashant/Documents/image/mainjava " + i + ".jpg");
37     fs=new FileOutputStream(f);
38      blob=rset.getBlob("SavePic");
39      b=blob.getBytes(1, (int)blob.length());
40      fs.write(b);
41     }
42 } 
43 
44 catch (SQLException e) 
45 {
46     System.err.println("Cannot connect ! ");
47     e.printStackTrace();
48 }
49 
50 finally {
51     System.out.println("Closing the connection.");
52     ps.close();
53     fs.close();
54     if (con != null) try { con.close(); } catch (SQLException ignore) {}
55 }
56 
57}
58}
59