Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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_weekendowa_20221008
Commits
2b9b3d97
Commit
2b9b3d97
authored
Nov 13, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lista0 - wersja skonfigurowana w aplikacji
parent
fe39c701
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
0 deletions
+54
-0
Lista0.java
...ernateNaSerwerze/src/main/java/sklep/serwlety/Lista0.java
+54
-0
No files found.
PC29-HibernateNaSerwerze/src/main/java/sklep/serwlety/Lista0.java
0 → 100644
View file @
2b9b3d97
package
sklep
.
serwlety
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
javax.servlet.ServletException
;
import
javax.servlet.annotation.WebServlet
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
sklep.model.Product
;
// http://localhost:8080/PC29-HibernateNaSerwerze-1.0/Lista0
@WebServlet
(
"/Lista0"
)
public
class
Lista0
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
response
.
setContentType
(
"text/plain"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
// W tej wersji obiekty JPA (emf i em) otwieram w sposób ogólny, tak jakby to była zwykła aplikacja.
// Nie zakładam, że jestem na serwerze.
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
em
=
emf
.
createEntityManager
();
out
.
println
(
"Mam połączenie. em = "
+
em
);
TypedQuery
<
Product
>
query
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
);
List
<
Product
>
products
=
query
.
getResultList
();
out
.
println
(
"Odczytano "
+
products
.
size
()
+
" produktów:"
);
for
(
Product
product
:
products
)
{
out
.
println
(
" * "
+
product
.
getProductName
()
+
" za "
+
product
.
getPrice
());
}
}
catch
(
Exception
e
)
{
out
.
println
(
"Katastrofa "
+
e
);
e
.
printStackTrace
(
out
);
}
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