import
java.io.*;
import
java.net.*;
import
java.sql.Blob;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.Statement;
import
javax.servlet.*;
import
javax.servlet.http.*;
/**
*
*
@author
mkarthik
*
@version
*/
public
class
ImageServlet
extends
HttpServlet
{
/**
* Processes requests for both HTTP <code>GET</code>
and <code>POST</code>
* methods.
*
* @param
request
* servlet request
* @param
response
* servlet response
*/
protected
void
doGet(HttpServletRequest
request,
HttpServletResponse
response) throws
ServletException,
IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter
out = response.getWriter();
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection
con = DriverManager.getConnection(
"jdbc:sqlserver://sql:1433;databaseName=karthik",
"karthik",
"karthik");
PreparedStatement
pstmt = con
.prepareStatement("insert
into image(image) " + "values(?)");
File
f = new
File("C:/image.jpg");
FileInputStream
fis = new
FileInputStream(f);
pstmt.setBinaryStream(1,
fis, (int)
f.length());
pstmt.execute();
con.commit();
Statement
stmt = con.createStatement();
ResultSet
rs = stmt.executeQuery("SELECT
image from image");
System.out.println("Image
is inserted successfully!");
Blob
b = null;
while
(rs.next()) {
b
= rs.getBlob("image");
}
byte
b1[] = new
byte[(int)
b.length()];
b1
= b.getBytes(1, (int)
b.length());
FileOutputStream
fos = new
FileOutputStream("D:/a.jpg");
fos.write(b1);
fos.close();
System.out.println("Image
is retrieved successfully!");
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
java 2d graphic examples
ReplyDeleteDrawing Shapes in Java examples