Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
96a88c76
Commit
96a88c76
authored
Apr 06, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
serwlet z produktami text/plain
parent
d05509f1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
0 deletions
+53
-0
Products0.java
PC25-SklepWeb/src/main/java/sklep/web/Products0.java
+53
-0
No files found.
PC25-SklepWeb/src/main/java/sklep/web/Products0.java
0 → 100644
View file @
96a88c76
package
sklep
.
web
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
@WebServlet
(
"/products0"
)
public
class
Products0
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
/* W tej wersji serwlet wypisuje dane produktów tekstowo.
* W kodzie serwletu bezpośrednio korzytstamy z technologii bazodanowej JDBC,
* mamy tutaj fragment SQL, nazwy tabel, kolumn, odczyt pojedynczych pól...
*
* Na dłuższą metę tak się nie programuje: byłoby za dużo kodu, mieszalibyśmy wartwę webową z warstwą danych,
* mielibyśmy dużo duplikacji kodu pomiędzy różnymi klasami w aplikacji.
*/
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
response
.
setContentType
(
"text/plain"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/sklep"
,
"kurs"
,
"abc123"
);
Statement
stmt
=
c
.
createStatement
();
ResultSet
rs
=
stmt
.
executeQuery
(
"SELECT * FROM products"
))
{
while
(
rs
.
next
())
{
out
.
printf
(
"Produkt nr %d: %s za cenę %s."
,
rs
.
getInt
(
"product_id"
),
rs
.
getString
(
"product_name"
),
rs
.
getBigDecimal
(
"price"
));
String
opis
=
rs
.
getString
(
"description"
);
if
(
opis
!=
null
)
{
out
.
printf
(
" Opis: %s."
,
opis
);
}
out
.
println
();
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
(
out
);
}
}
}
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