File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes How to store images into mysql using jsf and java coding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to store images into mysql using jsf and java coding" Watch "How to store images into mysql using jsf and java coding" New topic

How to store images into mysql using jsf and java coding

Ashraf Shaida

Joined: Oct 04, 2011
Posts: 1

Hello Every one .. I am very New in jsf.. I wrote some code for uploading image that are following......

This is JSF page Coding...

<%@taglib uri="" prefix="h"%>
<%@taglib uri="" prefix="f"%>
<%@ taglib uri="" prefix="t"%>
title>FileUpload example</title>
<h:form id="myform" enctype="multipart/form-data">
<t:inputFileUpload id="fileupload" value="#{filetestbean.fileup}"
size="20" />
<h:commandButton value="Submit"
action="#{filetestbean.savephoto}" />

///////////This is Bean Class ////////////////

public class Fileuploadtestbean {
private FileUpload Fileup;

public void setFileup(FileUpload fileup) {
Fileup = fileup;

public FileUpload getFileup() {
return Fileup;
}public String savephoto()


UserConnect cont = new UserConnect();
String st = cont.savephoto(this);
return "success";
catch (Exception e) {
// TODO: handle exception
return "st";

////////////////This is Class ////////////////

public class UserConnect {

ResourceBundle bundl = null;
String sum = "sum";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

public UserConnect() {
String driver = "";
bundl = ResourceBundle.getBundle("datasource");
String test = bundl.getString("ds.localhostrmm-ds.driver");

try {
String url = "jdbc:mysql://localhost/test?user=root&password=root";
try {
con = DriverManager.getConnection(url);

} catch (Exception e) {
// TODO: handle exception

} catch (Exception e) {
// TODO: handle exception

public String savephoto(Fileuploadtestbean bean)
UploadedFile upfile = null;
boolean success;
boolean failure;
PreparedStatement ps;

int image;
InputStream inputst = upfile.getInputStream();
long size =upfile.getSize();
System.out.println("File Size ==="+size);
byte [] buffer =new byte[(int)size];,0,(int)size);
String qry = "insert into application_table(image) values(?)";
ps = con.prepareStatement(qry);
System.out.println("image qry::" + qry);

ps.setInt(1, image);
success = true;
failure = false;
System.out.println("File Upload Successful.");

catch (Exception ioe) {
System.out.println("File Upload Unsuccessful.");
success = false;
failure = true;

This is a faces-config.xml configured file /////////


////and obviously I changed web.xml coding that is following....

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" version="3.0">





Set the size limit for uploaded files.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB

<!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages -->
<!-- servlet-name must match the name of your javax.faces.webapp.FacesServlet entry -->
<servlet-name>Faces Servlet</servlet-name>

<!-- extension mapping for serving page-independent resources (javascript, stylesheets, images, etc.) -->


But the problem when i am compiling and browse image and clicking into submit button .. there is nothing happing .. I am not able to find the error where its getting mistake or where is coding problem... So please any expert help I am positively expecting answer as soon as..


I agree. Here's the link:
subject: How to store images into mysql using jsf and java coding
It's not a secret anymore!