<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@ page language="java" %> <%@ page import="java.awt.image.*"%> <%@ page import="java.io.*"%> <%@ page import="java.sql.*"%> <%@ page import="java.io.File"%> <%@ page import="javax.imageio.ImageIO"%> <%@ page import="java.awt.image.BufferedImage,java.util.*"%> <%@ page import="java.awt.*"%> <%@ page import="java.util.*,com.oreilly.servlet.MultipartRequest"%> <% /* The Following Code is Used To Insert An Image Into Database */ String filename=""; try { //Download com.oreilly package MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024); Enumeration files=multi.getFileNames(); File f=null; while(files.hasMoreElements()) { String name=(String)files.nextElement(); filename=multi.getFilesystemName(name); String type=multi.getContentType(name); f=multi.getFile(name); System.out.println("The File is "+f); } Connection con=null; String userName="root"; String password = "veradis"; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample",userName,password); Statement stmt = con.createStatement(); InputStream is = new FileInputStream(f); byte b[]=new byte[is.available()]; is.read(b); int flag=0; try { String sql = "INSERT into tbl_image(image) values('" + b + "')"; System.out.println(sql); stmt.execute(sql); flag=1; } catch(Exception e) { System.out.println("SQL Exception : " + e); } if(flag==1) { System.out.println("Query Executed Successfully"); } stmt.close(); } catch(Exception e) { System.out.println(e); } out.println("The Image is Added into Database"); %>
Thursday, 10 January 2013
Upload an image into the database in java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment