Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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
javab_20230617
Commits
462f3052
Commit
462f3052
authored
Jul 16, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Controller używający EntityManager
parent
07041d27
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
15 deletions
+48
-15
ProductController_v2.java
...ep/alternatywne_dostepy_do_bazy/ProductController_v2.java
+34
-0
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+14
-15
No files found.
PC27-SklepSpring/src/main/java/sklep/alternatywne_dostepy_do_bazy/ProductController_v2.java
0 → 100644
View file @
462f3052
package
sklep
.
alternatywne_dostepy_do_bazy
;
import
jakarta.persistence.EntityManager
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
sklep.model.Product
;
import
java.util.List
;
@Controller
@RequestMapping
(
"/products_v2"
)
public
class
ProductController_v2
{
@Autowired
private
EntityManager
em
;
@GetMapping
(
produces
=
"text/plain;charset=UTF-8"
)
@ResponseBody
public
String
readAll
()
{
List
<
Product
>
products
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
).
getResultList
();
StringBuilder
sb
=
new
StringBuilder
(
"Lista produktów:\n"
);
for
(
Product
product
:
products
)
{
sb
.
append
(
" * "
)
.
append
(
product
.
getProductName
())
.
append
(
" za cenę "
)
.
append
(
product
.
getPrice
())
.
append
(
'\n'
);
}
return
sb
.
toString
();
}
}
PC27-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
462f3052
package
sklep
.
controller
;
package
sklep
.
controller
;
import
jakarta.persistence.EntityManager
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
sklep.model.Product
;
import
java.util.List
;
import
java.sql.*
;
@Controller
@Controller
@RequestMapping
(
"/products"
)
@RequestMapping
(
"/products"
)
public
class
ProductController
{
public
class
ProductController
{
@Autowired
private
EntityManager
em
;
@GetMapping
(
produces
=
"text/plain;charset=UTF-8"
)
@GetMapping
(
produces
=
"text/plain;charset=UTF-8"
)
@ResponseBody
@ResponseBody
public
String
readAll
()
{
public
String
readAll
()
{
List
<
Product
>
products
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
).
getResultList
();
StringBuilder
sb
=
new
StringBuilder
(
"Lista produktów:\n"
);
StringBuilder
sb
=
new
StringBuilder
(
"Lista produktów:\n"
);
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost:5432/sklep"
,
"kurs"
,
"abc123"
);
for
(
Product
product
:
products
)
{
Statement
stmt
=
c
.
createStatement
();
sb
.
append
(
" * "
)
ResultSet
rs
=
stmt
.
executeQuery
(
"SELECT * FROM products ORDER BY product_id"
))
{
.
append
(
product
.
getProductName
())
.
append
(
" za cenę "
)
while
(
rs
.
next
())
{
.
append
(
product
.
getPrice
())
sb
.
append
(
"* produkt "
)
.
append
(
'\n'
);
.
append
(
rs
.
getString
(
"product_name"
))
.
append
(
" za cenę "
)
.
append
(
rs
.
getBigDecimal
(
"price"
))
.
append
(
'\n'
);
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
sb
.
append
(
"Błąd: "
+
e
);
}
}
return
sb
.
toString
();
return
sb
.
toString
();
}
}
...
...
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