0% found this document useful (0 votes)
51 views

CR Eat Ecl Asses

This document describes how to create classes, database tables, and a mapping configuration file to manage employee data using Hibernate. It defines an Employee class with properties like id, first name, last name, and salary. An EMPLOYEE table is created in the database to match. A mapping file connects the Employee class to the EMPLOYEE table. Finally, an application class is created with methods to add, list, update, and delete employee records using Hibernate.

Uploaded by

alok patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

CR Eat Ecl Asses

This document describes how to create classes, database tables, and a mapping configuration file to manage employee data using Hibernate. It defines an Employee class with properties like id, first name, last name, and salary. An EMPLOYEE table is created in the database to match. A mapping file connects the Employee class to the EMPLOYEE table. Finally, an application class is created with methods to add, list, update, and delete employee records using Hibernate.

Uploaded by

alok patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Cr

eat
eCl
asses:

publ
i
cclassEmployee{
pri
vat
einti
d;
pri
vat
eStri
ngfir
stName;
pri
vat
eStri
nglastName;
pri
vat
eintsal
ary;

publ
icEmpl oyee(){}
publ
icEmpl oyee(Stringf name,St
ri
ngl name,
intsal
ary
){
thi
s.fi
rstName=f name;
thi
s.l
ast Name=l name;
thi
s.salary=sal ary;
}
publ
icintget I
d(){
ret
urni d;
}
publ
icvoi dsetId(inti d){
thi
s.i
d=i d;
}
publ
icStr i
ngget Fir
st Name( ){
ret
urnf ir
stName;
}
publ
icvoi dsetFirstName(St ri
ngf
irst
_name){
thi
s.fi
rstName=f i
r st_name;
}
publ
icStr i
ngget Last Name( ){
ret
urnl astName;
}
publ
icvoi dsetLastName(St ri
ngl
ast_name){
thi
s.l
ast Name=l ast _name;
}
publ
icintget Salary(){
ret
urnsal ary;
}
publ
icvoi dsetSalary (intsalar
y){
thi
s.salary=sal ary;
}
}
Cr
eat
eDat
abaseTabl
es:
createt abl
eEMPLOYEE(
idI NTNOTNULLaut o_i
ncrement,
first_nameVARCHAR( 20)defaul
tNULL,
last _
nameVARCHAR( 20)defaul
tNULL,
sal ary INTdefaultNULL,
PRI MARYKEY( id)
);

Cr
eat
eMappi
ngConf
igur
ati
onFi
le:
<?xml version=" 1.
0"encodi ng="utf-8"? >
<!
DOCTYPEhi bernate-mappi ngPUBLI C"-//Hibernat
e/Hi bernat
eMappi ngDTD/
/EN"
"http://www. hibernate.org/ dtd/hiber nate-mappi ng-
3.0.dtd">
<hiber nate-mappi ng>
<cl assname=" Empl oyee"t abl
e=" EMPLOYEE" >
<met aat tr
ibute="class- descr i
ption" >
Thi sclasscont ainst heempl oy eedet ail.
</ met a>
<idname=" id"type=" i
nt "column=" id">
<gener at
orcl ass="nat ive"/>
</ i
d>
<pr oper t
yname=" fi
rstName"col umn=" fi
rst_name"ty pe="st
ring"
/>
<pr oper t
yname=" l
ast Name"col umn=" l
ast_name"t y
pe=" str
ing"/
>
<pr oper t
yname=" salary "column=" salary"type="
int"
/>
</ class>
</hiber nate-mappi ng>
Cr
eat
eAppl
icat
ionCl
ass:
Finall
y,wewill
createourapplicat
ionclasswiththemain(
)
met hodt or
untheapplicat
ion.Wewi l
luset
hisappli
cat
ionto
savef ewEmployee'
sr ecordsandt henwewillappl
yCRUD
operationsonthoserecords.
i
mpor
tjav
a.ut
il
.Li
st;
i
mpor
tjav
a.ut
il
.Dat
e;
i
mpor
tjav
a.ut
il
.I
ter
ator
;

i
mpor
tor
g.hi
ber
nat
e.HibernateExcepti
on;
i
mpor
tor
g.hi
ber
nat
e.Session;
i
mpor
tor
g.hi
ber
nat
e.Transaction;
i
mpor
tor
g.hi
ber
nat
e.SessionFactory;
i
mpor
tor
g.hi
ber
nat
e.cfg.
Conf i
gurati
on;

publ
iccl assManageEmpl oyee{
pri
vatest at
icSessionFact oryfactory;
publicst ati
cv oidmain( String[]args){
t
ry{
fact ory=newConf igur ation().
confi
gure(
).buil
dSessi
onFactor
y()
;
}
catch( Throwableex ){
Sy stem. err
.pri
ntl
n("Fai l
edt ocr eat
esessionFactor
yobject
."+ex);
thrownewExcept ionI nIni
tiali
zerEr
ror(
ex);
}
ManageEmpl oyeeME=newManageEmpl oy ee()
;

/
*Addf ewempl
oyeerecor
dsi
ndatabase*/
I
ntegerempI
D1=ME.addEmpl
oyee("
Zara"
,"Ali
",1000);
I
ntegerempI
D2=ME.addEmpl
oyee("
Daisy
","Das",5000);
I
ntegerempI
D3=ME.addEmpl
oyee("
John","
Paul",10000);

/
*Li
stdownalltheempl
oyees*
/
ME.
li
stEmpl
oyees()
;

/
*Updateemployee'srecords*/
ME.
updateEmployee(empID1,5000)
;

/
*Deleteanemployeefr
om t
hedat
abase*
/
ME.
deleteEmpl
oyee(empI
D2)
;

/*Li
stdownnewli
stoft
heempl
oyees*
/
ME.l
ist
Employ
ees(
);
}
/*Met hodt oCREATEanempl oy eei nthedat abase* /
publ icInt egeraddEmpl oy ee(St r
ingf name, Stri
ngl name, intsalary)
{
Sessi onsessi on=f act ory.openSessi on( );
Transact iont x=nul l;
I
nt egerempl oyeeI D=nul l;
try{
tx=sessi on. begi nTr ansact i
on( );
Empl oy eeempl oy ee=newEmpl oy ee(fname, l
name, salary
);
empl oyeeI D=( I
nt eger )sessi on. save(empl oyee);
tx.commi t();
}catch( Hi bernat eExcept i
one){
i
f( tx!=nul l)tx.rollback( );
e.printSt ackTr ace( );
}fi
nal l
y{
sessi on. close( );
}
returnempl oyeeI D;
}
/*Met hodt oREADal ltheempl oy ees* /
publ icv oidl istEmpl oy ees() {
Sessi onsessi on=f act ory.openSessi on( );
Transact iont x=nul l;
try{
tx=sessi on. begi nTr ansact i
on( );
Listempl oy ees=sessi on.creat eQuer y ("
FROM Empl oy ee").
li
st()
;
for( It
er atori terator=
empl oyees. i
terator(); i
terator .
hasNext ();
){
Empl oy eeempl oy ee=( Empl oy ee)iterator.next(
);
Sy stem. out.print (
"FirstName: "+empl oyee.getFir
stName( ))
;
Sy stem. out.print (
"LastName: "+empl oyee.getLast Name( )
);
Sy stem. out.print l
n("Sal ary: "+empl oy ee.getSalary());
}
tx.commi t();
}catch( Hi bernat eExcept i
one){
i
f( tx!=nul l)tx.rollback( );
e.printSt ackTr ace( );
}fi
nal l
y{
sessi on. close( );
}
}
/*Met hodt oUPDATEsal aryf oranempl oy ee* /
publ icv oidupdat eEmpl oy ee( I
ntegerEmpl oyeeID, i
ntsalar y){
Sessi onsessi on=f act ory.openSessi on( );
Transact iont x=nul l;
try{
tx=sessi on. begi nTr ansact i
on( );
Empl oy eeempl oy ee=
(Empl oy ee) sessi on.get (Empl oyee. class,Empl oy eeI
D) ;
empl oyee. set Salar y(sal ary) ;
sessi on.update(employee);
tx.commi t();
}catch( Hibernat eExcept ione){
i
f( tx!=null
)t x.roll
back( );
e.printStackTr ace( )
;
}fi
nal l
y{
sessi on.close( );
}
}
/*Met hodt oDELETEanempl oyeefrom therecor
ds* /
publ icv oiddel eteEmpl oyee(IntegerEmployeeID){
Sessi onsessi on=f actory.
openSession();
Transact i
ont x=nul l;
try{
tx=sessi on. beginTr ansaction();
Empl oyeeempl oy ee=
(Empl oy ee) session.get(Employee.
class,
Empl oy
eeI
D);
sessi on.delet e(empl oyee);
tx.commi t();
}catch( Hibernat eExcept ione){
i
f( tx!=null
)t x.roll
back( );
e.printStackTr ace( )
;
}fi
nal l
y{
sessi on.close( );
}
}
}
$j
av aManageEmpl oyee
.
..
..
..VARI OUSLOGMESSAGESWI LLDISPLAYHERE. ..
..
..
.

Fi
rstName:
ZaraLastName:AliSal
ary:
1000
Fi
rstName:
DaisyLastName:DasSalary:
5000
Fi
rstName:
JohnLastName:PaulSalary:10000
Fi
rstName:
ZaraLastName:AliSal
ary:
5000
Fi
rstName:
JohnLastName:PaulSalary:10000

mysql>select*f r
om EMPLOYEE;
+--
--
+--
--
--
--
---
-+-
---
--
---
--
+---
--
--
-+
|i
d| f
ir
st_name| l
ast_name| sal
ary|
+--
--
+--
--
--
--
---
-+-
---
--
---
--
+---
--
--
-+
|29|Zara | Ali |5000|
|31|John | Paul |10000|
+--
--
+--
--
--
--
---
-+-
---
--
---
--
+---
--
--
-+
2rowsinset( 0.00sec

You might also like