Step 1: Create Database
Create database upload
Create Table
Create table Employee(Id int,Dp image primary key(Id))
Step 2: Create Persistent Class
public class Employee
{
private int id;
private byte[] dp;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public byte[] getDp() {
return dp;
}
public void setDp(byte[] dp) {
this.dp = dp;
}
}
For java.sql.Blob:
Create database upload
Create Table
Create table Employee(Id int,Dp image primary key(Id))
Step 2: Create Persistent Class
public class Employee
{
private int id;
private byte[] dp;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public byte[] getDp() {
return dp;
}
public void setDp(byte[] dp) {
this.dp = dp;
}
}
Step 3:Main.java
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;
public class Main {
public static void main(String ar[])
{
//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();
Employee e=new Employee();
e.setId(1);
File file=new File("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");
byte[] bytefile=new byte[(int)file.length()];
try
{
FileInputStream fs=new FileInputStream(file);
fs.read(bytefile);
fs.close();}
catch(Exception ex)
{
System.out.println(ex);
}
e.setDp(bytefile);
session.save(e);
t.commit();//transaction is committed
session.close();
System.out.println("success");}}
Step 4: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.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.connection.password">Password@12345</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=SI
</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Step 5: Employee.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 22, 2016 12:59:00 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="Employee" table="login">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="dp" column="dp" type="binary" />
</class>
</hibernate-mapping>
For java.sql.Blob:
<
property
name
=
"photo"
column
=
"PHOTO"
type
=
"blob"
/>
For primitive byte[] array:
<
property
name
=
"photo"
column
=
"PHOTO"
type
=
"binary"
/>
No comments:
Post a Comment