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
fcdb75d1
Commit
fcdb75d1
authored
Oct 04, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dostęp do bazy za pomocą JDBC
parent
fa062879
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
5 deletions
+53
-5
AlternatywneProdukty1.java
...src/main/java/sklep/controller/AlternatywneProdukty1.java
+49
-0
index.jsp
PC30-SklepSpring/src/main/webapp/index.jsp
+4
-5
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/AlternatywneProdukty1.java
0 → 100644
View file @
fcdb75d1
package
sklep
.
controller
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
javax.sql.DataSource
;
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
;
@Controller
@RequestMapping
(
"/alt1"
)
public
class
AlternatywneProdukty1
{
@Autowired
private
DataSource
dataSource
;
// Tutaj użyjemy "zwykłego JDBC", czyli PreparedStatemnt, ResultSet itd...
// Ale Spring automatycznie wstrzyknie nam (jako DataSource) połączenie z bazą daych na podstawie konfiguracji.
@GetMapping
(
path
=
"/products"
,
produces
=
"text/plain;charset=UTF-8"
)
@ResponseBody
public
String
products
()
{
final
String
sql
=
"SELECT product_name, price FROM products ORDER BY product_id"
;
try
(
Connection
c
=
dataSource
.
getConnection
();
PreparedStatement
stmt
=
c
.
prepareStatement
(
sql
);
ResultSet
rs
=
stmt
.
executeQuery
())
{
StringBuilder
tekst
=
new
StringBuilder
(
"Lista produktów:\n"
);
while
(
rs
.
next
())
{
tekst
.
append
(
" * "
);
tekst
.
append
(
rs
.
getString
(
1
));
tekst
.
append
(
" za cenę "
);
tekst
.
append
(
rs
.
getString
(
2
));
tekst
.
append
(
'\n'
);
}
return
tekst
.
toString
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
return
"Problem z odczytaniem produktów: "
+
e
;
}
}
}
PC30-SklepSpring/src/main/webapp/index.jsp
View file @
fcdb75d1
...
...
@@ -42,11 +42,10 @@
<h2>
Alternatywne dostępy do bazy danych
</h2>
<ul>
<li><a
href=
"/dodatkowe/sql"
>
Dostęp JDBC
</a>
(wstrzykiwanie DataSource)
</li>
<li><a
href=
"/dodatkowe/jpa"
>
Dostęp Hibernate/JPA
</a>
(wstrzykiwanie EntityManager)
</li>
<li><a
href=
"/dodatkowe/products"
>
products
</a>
(wstrzykiwanie EntityManager)
</li>
<li><a
href=
"/dodatkowe/products/1"
>
jeden produkt
</a></li>
<li><a
href=
"/dodatkowe/by_price?min=1000&max=2900"
>
by price
</a></li>
<li><a
href=
"/alt1/products"
>
Dostęp JDBC
</a>
(wstrzykiwanie DataSource)
</li>
<li><a
href=
"/alt2/products"
>
Dostęp Hibernate/JPA
</a>
(wstrzykiwanie EntityManager)
</li>
<li><a
href=
"/alt2/products/1"
>
jeden produkt
</a></li>
<li><a
href=
"/alt2/products/by_price?min=1000&max=2900"
>
by price
</a></li>
</ul>
</body>
...
...
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