import java.io.*; import java.sql.*; class InsertImages{ public static void main(String[] args) { try{ InputStream sImage; PreparedStatement pre=null; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp", "root", "root" ); String imagename[]={"c:/1.jpg","c:/2.jpg","c:/3.jpg","c:/4.jpg"}; File imgfile[] = new File[4]; for(int i=0;i<imgfile.length;i++){ imgfile[i]=new File(imagename[i]); FileInputStream fin = new FileInputStream(imgfile[i]); pre = con.prepareStatement("insert into myfile( file_name, file_data) values ( ?, ?)"); pre.setString(1,imgfile[i].getName()); pre.setBinaryStream(2,fin,(int)imgfile[i].length()); pre.executeUpdate(); } System.out.println("Inserting Successfully!"); pre.close(); con.close(); } catch(Exception ex){ System.out.println("error :"+ex); } } }
Sunday, October 14, 2012
loading images in mysql database
Subscribe to:
Post Comments (Atom)
This comment has been removed by a blog administrator.
ReplyDelete