Step 1: Create Database
import java.sql.Blob;
public class pojo {
private Blob image;
public Blob getImage() {
return image;
}
public void setImage(Blob image) {
this.image = image;
}}
Step 3: Create cfg and hbm xml files
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="pojo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
pojo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 27, 2016 4:54:07 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="pojo" table="POJO">
<id name="image" type="java.sql.Blob">
<column name="IMAGE" />
<generator class="assigned" />
</id>
</class>
</hibernate-mapping>
hibernate.reveng.xml
CREATE TABLE `upload_image` (`IMAGE` longblob NULL DEFAULT NULL )
Step 2: Create persistent class
public class pojo {
private Blob image;
public Blob getImage() {
return image;
}
public void setImage(Blob image) {
this.image = image;
}}
Step 3: Create cfg and hbm xml files
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="pojo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
pojo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 27, 2016 4:54:07 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="pojo" table="POJO">
<id name="image" type="java.sql.Blob">
<column name="IMAGE" />
<generator class="assigned" />
</id>
</class>
</hibernate-mapping>
hibernate.reveng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-catalog="test" match-name="upload_image"/>
</hibernate-reverse-engineering>
Step 4: Create Action class
uploadingServlet.java
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.io.File;
import java.io.FileInputStream;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 1216584) // upload file's size up to 16MB
public class uploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("photo");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
try {
//creating configuration object
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
//creating session factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
// constructs SQL statement
PreparedStatement statement = session.connection().prepareStatement("INSERT INTO upload_image values (?)");
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(1, inputStream);
}
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
t.commit();
session.close();
System.out.println("Saved");
System.out.println("File uploaded and saved into database");
}
}
catch (SQLException ex)
{
System.out.println("ERROR: " + ex.getMessage());
ex.printStackTrace();
} }}
No comments:
Post a Comment