In this Tutorial, our concern is to make you learn storing and also retrieving Image (Blob data) using Struts2, Oracle 10g and Eclipse. but we are also storing and retrieving Numeric and alphanumeric data which is a plus point of this tutorial.
Here we are using Apache Tomcat 7.0 and Java 6 but you can use them as other versions too available on your system.
you can also Download full code with required Jars.
Download Full Code with Jars
Complete Project View in Project Explorer
Create table(imgtab) in Oracle database we are here using hr schema you can use other also.
Step 1 : Code for storing data into Database:
p1.jsp
This is page for user's input
Post1Action.java
This is a POJO class and it is working as action class for Struts.
success.jsp
This page will display on successful submission of data.
Step 2: Code for retrieve data from Database:
search.jsp
This page will display all data you stored in Database Table.
image.jsp
This page code used to retrieve image from database table.
web.xml
struts.xml
This file contains information about which action class to be invoked.
Download Full Code with Jars
Output:
Here we are using Apache Tomcat 7.0 and Java 6 but you can use them as other versions too available on your system.
you can also Download full code with required Jars.
Download Full Code with Jars
Complete Project View in Project Explorer
Create table(imgtab) in Oracle database we are here using hr schema you can use other also.
imgtab table structure |
imgtab table with Stored Data |
Step 1 : Code for storing data into Database:
p1.jsp
This is page for user's input
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Post Image and Data</title>
</head>
<body>
<h4 align=right><a href=search.jsp>See your Data</a></h4><hr>
<center>
<form action="addpost1" enctype="multipart/form-data" method="post">
<table>
<tr><td>Enter Id:</td><td><input type="text" name="id"></td></tr>
<tr><td>Enter Category:</td><td><input type="text" name="cat"></td></tr>
<tr><td>Add Photo: </td><td><input type="file" name="pic"></td></tr>
<tr><td></td><td><input type="submit" value="Post"></td></tr>
</table>
</form>
</center>
</body>
</html>
Post1Action.java
This is a POJO class and it is working as action class for Struts.
package com.blog.webideaworld;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.annotation.MultipartConfig;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
@MultipartConfig
public class Post1Action extends ActionSupport {
int id;
String cat;
File pic;
public String execute() throws Exception
{
// Connect to Oracle
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
con.setAutoCommit(false);
System.out.println(pic.getPath());
FileInputStream pic1 = new FileInputStream(pic.getPath());
PreparedStatement ps = con.prepareStatement("insert into imgtab(id,category,photo) values(?,?,?)");
ps.setInt(1, id);
ps.setString(2, cat);
// size must be converted to int otherwise it results in error
ps.setBinaryStream(3, pic1, (int) pic1.available());
int i=ps.executeUpdate();
con.commit();
con.close();
if(i!=0)
return SUCCESS;
else
return INPUT;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCat() {
return cat;
}
public void setCat(String cat) {
this.cat = cat;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
}
success.jsp
This page will display on successful submission of data.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4 align=right><a href=search.jsp>See your Data</a></h4><hr>
<center>
Record successfully submitted.</center>
</body>
</html>
Step 2: Code for retrieve data from Database:
search.jsp
This page will display all data you stored in Database Table.
<%@page import="java.sql.*"%>
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Your Uploaded Data</title>
</head>
<body>
<center>
<table border="1"><tr align="center"><td>Photo</td><td>Customer Id</td><td>Category</td></tr>
<%
Class.forName("oracle.jdbc.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from imgtab");
while( rs.next()) {
int k=rs.getInt(1);
%>
<tr align="center">
<td><img width=200 height=150 src=image.jsp?idd=<%=k%> ></img></td>
<td><%=rs.getString("id")%></td>
<td><%=rs.getString("category")%></td>
</tr>
<%
}
%>
</table>
</center>
</body>
</html>
image.jsp
This page code used to retrieve image from database table.
<%@ page import="java.sql.*,java.io.*,java.util.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>post</title>
</head>
<body>
<%
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
PreparedStatement ps = con.prepareStatement("select photo from imgtab where id = ?");
String idd = request.getParameter("idd");
int id=Integer.parseInt(idd);
System.out.print(id);
ps.setInt(1,id );
ResultSet rs = ps.executeQuery();
rs.next();
Blob b = rs.getBlob("photo");
response.setContentType("image/jpeg");
response.setContentLength((int) b.length());
InputStream is = b.getBinaryStream();
OutputStream os = response.getOutputStream();
byte buf[] = new byte[(int) b.length()];
is.read(buf);
os.write(buf);
os.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
%>
</body>
</html>
web.xml
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>StoreImageDB</display-name>
<welcome-file-list>
<welcome-file>p1.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
This file contains information about which action class to be invoked.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="log" namespace="/" extends="struts-default">
<action name="addpost1" class="com.blog.webideaworld.Post1Action" method="execute">
<result name="success">success.jsp</result>
<result name="input">p1.jsp</result>
</action>
</package>
</struts>
Download Full Code with Jars
Output:
For Storing data |
Data Retrieved from database |
More Struts Topics :
- How to Set Struts2 Environment for Eclipse with Oracle 10g Express Edition
- Simple struts2 program with Eclipse & Apache Tomcat Server
- Login Using Struts2 and Eclipse
- Database Access using Struts2,Eclipse and Oracle
- Basic form Validation using Struts2 Framework with validate() method and ActionSupport class
- Validation using Struts2 Framework and XML file on Radio button and DropDownList with Eclipse
- Validation using Struts2 Framework and XML file with Eclipse Indigo
- AJAX validation using Struts2 Framework and XML file with Eclipse Indigo
- Session Tracking using SessionAware Interface with Struts2, Oracle Database and Eclipse IDE
- Action Wildcards with Struts2
- How to Configure methods in Action mappings with or without Wildcards using Struts2
- Store and Retrieve Numeric, Alphanumeric and Blob (Image) data with Struts2, Oracle 10g XE and Eclipse Indigo as IDE
- How to use Struts2 Iterator tag to get Users Details with Oracle 10g XE and Eclipse
- How to iterate user details through ArrayList with Iterator tag in Struts2 using Oracle 10g XE and Eclipse
- How to upload Multiple Files- Images (blob data) with enctype="multipart/form-data" in Oracle Database using Struts2, JDBC and Eclipse IDE
No comments:
Post a Comment