Uploading an Image to a Database using JSP

By: Mahesh Emailed: 1612 times Printed: 2079 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This jsp Page is Uploding The Image into Database using MultipartRequest class FileSystem concepts and ByteArray.

<%@ page import="java.sql.*,java.io.*,java.util.*,com.oreilly.servlet.MultipartRequest,com.microsoft.jdbc.sqlserver.SQLServerDriver;"%>
<%

/* 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);
}
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver;databasename="xxx","username","password");
Statement stmt = con.createStatement();
InputStream is = new FileInputStream(f);
byte b[]=new byte[is.available()];
is.read(b);
String sql = "INSERT into photo_test (\"Photo\") values('" + b + "')";
stmt.execute(sql);
stmt.close();
}catch(Exception e)
{
System.out.println(e);
}
out.println("The Image is Added into Database");


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(4)


1. View Comment

GOOD FOR BIGGENERS

View Tutorial          By: naresh at 2012-08-04 11:19:03
2. View Comment

good for learners

View Tutorial          By: muhammed at 2012-11-20 10:38:28
3. View Comment

feel happy but i want to database tables also

View Tutorial          By: sisira at 2013-06-05 13:26:51
4. View Comment

how to code for images hitting in jsp like google images...

please tell any hints... please


View Tutorial          By: Kumaravel at 2014-08-06 11:23:08

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Mahesh
Floating point precision in PHP
Uploading an Image to a Database using JSP

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Alert Example
JSP Example to connect to MS SQL database and retrieve records
Sending Email using JSP
JSP Program for display Date
Uploading an Image to a Database using JSP
Form processing in JSP
Retrieving a Portion of a String
Using a DataSource from WebLogic in a JSP
JSP CheckBox Example
The JSP Program running first Time.
Embedding java codes in jsp sciptlets
JavaScript Security
The Web User's Perspective
JSP Tags for SQL to connect to a database
What are the different scopes in JSP?
Most Emailed Articles (in JSP)
Embedding java codes in jsp sciptlets
The taglib, tag, include, attribute and the variable Directive in JSP
Packaging Servlets
The Advantages of JSP
What are the different scopes in JSP?
Sessions in JSP
Server-side plug-Ins
Retrieving a Portion of a String
The Out Object in JSP
A JSP page that gets properties from a bean
JSP Alert Example
The Advantages of Servlets Over “Traditional” CGI
Web Security Issues
The Response Object in JSP
Uploading an Image to a Database using JSP