Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
stadler2
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
stadler2
Commits
f993259a
Commit
f993259a
authored
Dec 10, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pierwsze dostępy do bazy danych
parent
e93ed8b1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
131 additions
and
2 deletions
+131
-2
AProducts.java
quarkus2/src/main/java/sklep/rest/AProducts.java
+34
-0
BProducts.java
quarkus2/src/main/java/sklep/rest/BProducts.java
+37
-0
CProducts.java
quarkus2/src/main/java/sklep/rest/CProducts.java
+56
-0
application.properties
quarkus2/src/main/resources/application.properties
+4
-2
No files found.
quarkus2/src/main/java/sklep/rest/AProducts.java
0 → 100644
View file @
f993259a
package
sklep
.
rest
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.WebApplicationException
;
import
jakarta.ws.rs.core.MediaType
;
import
java.sql.*
;
@Path
(
"/a/products"
)
public
class
AProducts
{
@GET
@Produces
(
MediaType
.
TEXT_PLAIN
)
public
String
allProducts
()
{
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost:5432/sklep"
,
"alx"
,
"abc123"
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
"select product_name, price from products"
))
{
StringBuilder
sb
=
new
StringBuilder
(
"Produkty:\n"
);
try
(
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
sb
.
append
(
rs
.
getString
(
1
)).
append
(
" za "
).
append
(
rs
.
getString
(
2
)).
append
(
'\n'
);
}
}
return
sb
.
toString
();
}
catch
(
SQLException
e
)
{
throw
new
WebApplicationException
(
"Błąd bazy danych. "
+
e
.
getMessage
(),
e
,
500
);
}
}
}
\ No newline at end of file
quarkus2/src/main/java/sklep/rest/BProducts.java
0 → 100644
View file @
f993259a
package
sklep
.
rest
;
import
jakarta.inject.Inject
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.WebApplicationException
;
import
jakarta.ws.rs.core.MediaType
;
import
javax.sql.DataSource
;
import
java.sql.*
;
@Path
(
"/b/products"
)
public
class
BProducts
{
@Inject
private
DataSource
dataSource
;
// ew. adnotacja @Resource
@GET
@Produces
(
MediaType
.
TEXT_PLAIN
)
public
String
allProducts
()
{
try
(
Connection
c
=
dataSource
.
getConnection
();
PreparedStatement
stmt
=
c
.
prepareStatement
(
"select product_name, price from products"
))
{
StringBuilder
sb
=
new
StringBuilder
(
"Produkty:\n"
);
try
(
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
sb
.
append
(
rs
.
getString
(
1
)).
append
(
" za "
).
append
(
rs
.
getString
(
2
)).
append
(
'\n'
);
}
}
return
sb
.
toString
();
}
catch
(
SQLException
e
)
{
throw
new
WebApplicationException
(
"Błąd bazy danych. "
+
e
.
getMessage
(),
e
,
500
);
}
}
}
\ No newline at end of file
quarkus2/src/main/java/sklep/rest/CProducts.java
0 → 100644
View file @
f993259a
package
sklep
.
rest
;
import
jakarta.inject.Inject
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.WebApplicationException
;
import
jakarta.ws.rs.core.MediaType
;
import
javax.sql.DataSource
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
@Path
(
"/c/products"
)
public
class
CProducts
{
@Inject
private
DataSource
dataSource
;
// ew. adnotacja @Resource
@GET
@Produces
(
MediaType
.
TEXT_HTML
)
public
String
allProducts
()
{
try
(
Connection
c
=
dataSource
.
getConnection
();
PreparedStatement
stmt
=
c
.
prepareStatement
(
"select product_name, price from products"
))
{
StringBuilder
result
=
new
StringBuilder
(
"""
<!DOCTYPE html>
<html>
<body>
<h1>Lista produktów</h1>
<ul>
"""
);
try
(
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
result
.
append
(
"<li>"
)
.
append
(
rs
.
getString
(
"product_name"
))
.
append
(
" za "
)
.
append
(
rs
.
getString
(
"price"
))
.
append
(
"</li>\n"
);
}
}
result
.
append
(
"""
</ul>
</body>
</html>
"""
);
return
result
.
toString
();
}
catch
(
SQLException
e
)
{
throw
new
WebApplicationException
(
"Błąd bazy danych. "
+
e
.
getMessage
(),
e
,
500
);
}
}
}
\ No newline at end of file
quarkus2/src/main/resources/application.properties
View file @
f993259a
quarkus.datasource.jdbc.url
=
jdbc:postgresql://vps-2bc225bd.vps.ovh.net:5432/sklep
quarkus.datasource.username
=
alx
quarkus.datasource.password
=
abc123vps
quarkus.datasource.jdbc.url
=
jdbc:postgresql://localhost:5432/sklep
quarkus.datasource.password
=
abc123
# quarkus.datasource.jdbc.url=jdbc:postgresql://vps-2bc225bd.vps.ovh.net:5432/sklep
# quarkus.datasource.password=abc123vps
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