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
d9a1f228
Commit
d9a1f228
authored
Sep 28, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Odczyt produktów - wersja 0
parent
bc4acf0a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
pom.xml
PC26-SklepWeb/pom.xml
+7
-0
Products0.java
PC26-SklepWeb/src/main/java/sklep/web/Products0.java
+53
-0
No files found.
PC26-SklepWeb/pom.xml
View file @
d9a1f228
...
@@ -29,11 +29,18 @@
...
@@ -29,11 +29,18 @@
<groupId>
javax
</groupId>
<groupId>
javax
</groupId>
<artifactId>
javaee-web-api
</artifactId>
<artifactId>
javaee-web-api
</artifactId>
<version>
8.0.1
</version>
<version>
8.0.1
</version>
<scope>
provided
</scope>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<groupId>
javax.servlet
</groupId>
<artifactId>
jstl
</artifactId>
<artifactId>
jstl
</artifactId>
<version>
1.2
</version>
<version>
1.2
</version>
</dependency>
</dependency>
<dependency>
<groupId>
org.postgresql
</groupId>
<artifactId>
postgresql
</artifactId>
<version>
42.5.0
</version>
<scope>
runtime
</scope>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
PC26-SklepWeb/src/main/java/sklep/web/Products0.java
0 → 100644
View file @
d9a1f228
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