Uploading an Image to a Database using JSP

By: Mahesh Emailed: 1585 times Printed: 2037 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 Example to connect to MS SQL database and retrieve records
JSP Program for display Date
JSP CheckBox Example
What are the different scopes in JSP?
Comparison operators in JSP
JSP Alert Example
The Advantages of JSP
Sending Email using JSP
Uploading an Image to a Database using JSP
Arithmetic Evaluation Using the Expression Language in JSP
Tags using in jsp
Writing your first JSP page
Automatically Refreshing a JSP
JSP Example to connect to MS SQL database using Tomcat Connection Pool
Calling JSP from a batch file
Most Emailed Articles (in JSP)
Click to Activate and Use this control
NotifyUtil::java.net.ConnectException: Connection refused: connect
Document Object
Comparison operators in JSP
The Servlet Life Cycle
Cookies using JSP or Java Bean
Embedding java codes in jsp sciptlets
JSP pages in servlet
JSP Program for display Date
HttpClient
The Advantages of Servlets Over “Traditional” CGI
Closing Windows
Web Security Issues
Disabling Scriptlets in JSP using web.xml
Enable/Disable Scripting Elements in JSP