Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
java_dzienna_15_09
Commits
9f434a62
Commit
9f434a62
authored
Sep 30, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Edycja danych Hibernatem w bazie sklep
parent
a5311045
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
171 additions
and
0 deletions
+171
-0
DodajProdukt.java
...Hibernate/src/main/java/sklep/przyklady/DodajProdukt.java
+84
-0
EdycjaDanych.java
...Hibernate/src/main/java/sklep/przyklady/EdycjaDanych.java
+87
-0
No files found.
PC27-Hibernate/src/main/java/sklep/przyklady/DodajProdukt.java
0 → 100644
View file @
9f434a62
package
sklep
.
przyklady
;
import
java.math.BigDecimal
;
import
java.util.Locale
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
sklep.model.Product
;
public
class
DodajProdukt
{
public
static
void
main
(
String
[]
args
)
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
scanner
.
useLocale
(
Locale
.
US
);
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Początek transakcji"
);
em
.
getTransaction
().
begin
();
while
(
true
)
{
System
.
out
.
print
(
"Podaj nazwę nowego produktu (pusty napis, aby zakończyć): "
);
String
name
=
scanner
.
nextLine
();
if
(
name
.
isEmpty
())
break
;
System
.
out
.
print
(
"Podaj cenę: "
);
BigDecimal
price
=
scanner
.
nextBigDecimal
();
scanner
.
nextLine
();
System
.
out
.
print
(
"Podaj stawkę VAT, np 23 : "
);
int
vat
=
scanner
.
nextInt
();
scanner
.
nextLine
();
System
.
out
.
print
(
"Podaj opis: "
);
String
description
=
scanner
.
nextLine
();
if
(
description
.
isEmpty
())
{
description
=
null
;
}
// aby wpisać rekord do bazy, tworzymy obiekt bez określonego ID
Product
product
=
new
Product
();
product
.
setProductName
(
name
);
product
.
setPrice
(
price
);
product
.
setVat
(
BigDecimal
.
valueOf
(
vat
).
movePointLeft
(
2
));
product
.
setDescription
(
description
);
System
.
out
.
printf
(
"Produkt przed persist: id: %s, name: %s, price: %s, vat: %s, desc: %s\n"
,
product
.
getProductId
(),
product
.
getProductName
(),
product
.
getPrice
(),
product
.
getVat
(),
product
.
getDescription
());
// Dodanie obiektu do puli obiektów zarządzanych i wykonanie INSERT, ale jeszcze bez COMMIT
em
.
persist
(
product
);
System
.
out
.
printf
(
"Produkt po persist: id: %s, name: %s, price: %s, vat: %s, desc: %s\n"
,
product
.
getProductId
(),
product
.
getProductName
(),
product
.
getPrice
(),
product
.
getVat
(),
product
.
getDescription
());
System
.
out
.
println
();
}
System
.
out
.
println
(
"Czy zapisać zmiany? [T/N]"
);
String
wybor
=
scanner
.
next
().
toUpperCase
();
switch
(
wybor
)
{
case
"T"
:
System
.
out
.
println
(
"Zatwierdzam transakcję"
);
em
.
getTransaction
().
commit
();
break
;
case
"N"
:
System
.
out
.
println
(
"Cofam transakcję"
);
em
.
getTransaction
().
rollback
();
break
;
default
:
System
.
out
.
println
(
"Rozłączam się bez zatwierdzenia transakcji"
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC27-Hibernate/src/main/java/sklep/przyklady/EdycjaDanych.java
0 → 100644
View file @
9f434a62
package
sklep
.
przyklady
;
import
java.math.BigDecimal
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.EntityTransaction
;
import
javax.persistence.Persistence
;
import
sklep.model.Product
;
public
class
EdycjaDanych
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Rozpoczynam transakcję"
);
EntityTransaction
transakcja
=
em
.
getTransaction
();
transakcja
.
begin
();
System
.
out
.
println
(
"Aby zakończyć, podaj liczbę 0"
);
while
(
true
)
{
System
.
out
.
print
(
"\nPodaj id produktu: "
);
int
id
=
sc
.
nextInt
();
if
(
id
==
0
)
break
;
Product
product
=
em
.
find
(
Product
.
class
,
id
);
if
(
product
==
null
)
{
System
.
out
.
println
(
"Nie ma produktu o id "
+
id
);
continue
;
}
System
.
out
.
println
(
"Odczytany produkt: "
+
product
);
System
.
out
.
println
(
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
()
+
", opis: "
+
product
.
getDescription
());
// Umożliwiamy wprowadzenie zmian i te zmiany są nakładane za pomocą setterów bezpośrednio na obiekcie encji.
System
.
out
.
println
(
"Podaj zmianę ceny: "
);
BigDecimal
zmiana
=
sc
.
nextBigDecimal
();
sc
.
nextLine
();
if
(
zmiana
.
compareTo
(
BigDecimal
.
ZERO
)
!=
0
)
{
product
.
setPrice
(
product
.
getPrice
().
add
(
zmiana
));
}
System
.
out
.
println
(
"Podaj nową nazwę (enter, aby nie zmieniać): "
);
String
nazwa
=
sc
.
nextLine
();
if
(!
nazwa
.
isEmpty
())
{
product
.
setProductName
(
nazwa
);
}
System
.
out
.
println
(
"Podaj nowy opis (enter, aby nie zmieniać): "
);
String
opis
=
sc
.
nextLine
();
if
(!
opis
.
isEmpty
())
{
product
.
setDescription
(
opis
);
}
}
// Do tej pory zmiany zostały dokonany w obiektach znajdujących się w pamięci.
// Aby zapisać zmiany w bazie danych, należy zatwierdzić transakcję (i tylko tyle).
// To zapisze wszystkie "obiekty zarządzane" przez EntityManagera.
System
.
out
.
println
(
"Czy zapisać zmiany? [T/N] "
);
String
taknie
=
sc
.
nextLine
().
trim
().
toUpperCase
();
switch
(
taknie
)
{
case
"T"
,
"Y"
->
{
transakcja
.
commit
();
System
.
out
.
println
(
"Zmiany zatwierdzone"
);
}
case
"N"
->
{
transakcja
.
rollback
();
System
.
out
.
println
(
"Zmiany wycofane"
);
}
// w pozostałych przypadkach nie ma ani commit, ani rollback → zmiany nie zostaną zapisane
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment