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
18edc82e
Commit
18edc82e
authored
Oct 23, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pierwszy serwlet do produktów
parent
f4a93912
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 @
18edc82e
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
javax.servlet.ServletException
;
import
javax.servlet.annotation.WebServlet
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.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