• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store images into mysql using jsf and java coding

Ashraf Shaida
Posts: 1
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" 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 = "org.gjt.mm.mysql.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];
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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 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..


  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic