File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Spring in Action this week in the Spring 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
Author

How to store images into mysql using jsf and java coding

Ashraf Shaida
Greenhorn

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="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"%>
<html>
<head>
title>FileUpload example</title>
</head>
<body>
<f:view>
<h:form id="myform" enctype="multipart/form-data">
<t:inputFileUpload id="fileupload" value="#{filetestbean.fileup}"
size="20" />
<h:commandButton value="Submit"
action="#{filetestbean.savephoto}" />
</h:form>
</f:view>
</body>
</html>

///////////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()

{
try
{

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 {
Class.forName(driver).getInterfaces();
String url = "jdbc:mysql://localhost/test?user=root&password=root";
try {
con = DriverManager.getConnection(url);

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

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

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

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

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

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

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


<managed-bean>
<managed-bean-name>filetestbean</managed-bean-name>
<managed-bean-class>bean.Fileuploadtestbean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

////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">

<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>


<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>


<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>


<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>
org.apache.myfaces.webapp.filter.ExtensionsFilter
</filter-class>
<init-param>
<description>
Set the size limit for uploaded files.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB
</description>
<param-name>maxFileSize</param-name>
<param-value>20m</param-value>
</init-param>
</filter>

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

<!-- extension mapping for serving page-independent resources (javascript, stylesheets, images, etc.) -->
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>

</web-app>


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..

Thanks


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store images into mysql using jsf and java coding