Servlet Code to Insert Image into Database

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ImageInsertInTable extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:odbc:test";
Connection con=null;
try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

con = DriverManager.getConnection(connectionURL);

PreparedStatement ps = con.prepareStatement("INSERT INTO table1 VALUES(?,?)");
File file = new File("C:/Documents and Settings/MOHSIN/Desktop/WebApplication4/web/Wallpaper.jpg");
FileInputStream fs = new FileInputStream(file);
ps.setInt(1,8);
ps.setBinaryStream(2,fs,fs.available());
int i = ps.executeUpdate();
pw.print("<h1>"+ i +"</h1>");
if(i!=0){
pw.println("image inserted successfully");
}
else{
pw.println("problem in image insertion");
}
}
catch (Exception e){
pw.print("<h1>"+e+"</h1>");
}
}
}

Comments

Popular posts from this blog

Edit/Modify existing PDF using Java(iText)

Verify Digital Signature in PDF using Java(iText) and cacerts

Steps to create SOAP Web service using Eclipse