AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



struts-hibernate: Insert image and date in Oracle database using struts and hibernate

I need a code for inserting date & image in oracle data base. plz forward the code with exsmple.

struts x 31
hibernate x 23
Posted On : 2016-08-10 10:15:09.0
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
266150
up-rate
5
down-rate

Answers


First create one table in Oracle DB using below query:

CREATE TABLE "IMGTABLE"
( "ID" NUMBER NOT NULL,
"NAME" VARCHAR2(4000),
"PHOTO" BLOB
)
/

Let´s see the Project Structure and place the files at appropriate place:


web.xml:
----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>

index.jsp:
-------------------------------
<%@ taglib uri="/struts-tags" prefix="s"%>
<s:form action="saveImage" method="post" enctype="multipart/form-data">
<s:textfield name="image.imageName" title="Image Name" label="Image Name"></s:textfield>
<s:file name="image.image"></s:file>
<s:submit name="submit" value="Submit"></s:submit>
</s:form>

success.jsp:
---------------------------
successfully uploaded!!

error.jsp:
----------------------------
Something Went Wrong!!!

struts.xml:
--------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="axcf" extends="struts-default">
<action name="saveImage" class="in.anyforum.ImageAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>

hibernate.cfg.xml:
--------------------------------------------------
<?xml version=´1.0´ encoding=´UTF-8´?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="ImageFile.hbm.xml"/>
</session-factory>

</hibernate-configuration>

ImageFile.hbm.xml:
-------------------------------------------------
<?xml version=´1.0´ encoding=´UTF-8´?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="in.anyforum.ImageFile" table="Imgtable">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="imageName" column="name"></property>
<property name="image" column="photo"></property>
</class>

</hibernate-mapping>

ImageAction.java:
------------------------------------------------
package in.anyforum;

import com.opensymphony.xwork2.ActionSupport;

public class ImageAction extends ActionSupport{
private ImageFile image;

public ImageFile getImage() {
return image;
}

public void setImage(ImageFile image) {
this.image = image;
}

public String execute(){
System.out.println(image.getImage());
int status=ImageDao.saveImage(image);
System.out.println(status);
if(status>0){
return "success";
}else{
return "error";
}
}
}

ImageFile.java:
-------------------------------------------------
package in.anyforum;

import java.io.File;

public class ImageFile {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String imageName;
private File image;
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}

}


ImageDao.java:
---------------------------------------
package in.anyforum;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class ImageDao {
public static int saveImage(ImageFile image){
SessionFactory sessionFactory=null;
Session session=null;
Transaction tx=null;
try{
sessionFactory =new Configuration().configure().buildSessionFactory();
session=sessionFactory.openSession();
tx=session.beginTransaction();
int status= (Integer)session.save(image);
tx.commit();
return status;
}catch (Exception e) {
tx.rollback();
System.out.println(e);
return 0;
}
}
}


Note:
-----------------------
Don´t forget to add ojdbc14.jar file

Click Here to download the project using My Eclipse IDE

Posted On : 2016-08-11 00:43:42
Satisfied : 1 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319307
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in