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
f4a93912
Commit
f4a93912
authored
Oct 23, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Podstawowe przykłady dostępu do bazy sklep
parent
a3dd1d77
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
112 additions
and
0 deletions
+112
-0
DodajProdukt.java
...klepWeb/src/main/java/dostep_podstawowy/DodajProdukt.java
+79
-0
WypiszProdukty.java
...epWeb/src/main/java/dostep_podstawowy/WypiszProdukty.java
+33
-0
No files found.
PC25-SklepWeb/src/main/java/dostep_podstawowy/DodajProdukt.java
0 → 100644
View file @
f4a93912
package
dostep_podstawowy
;
import
java.math.BigDecimal
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.util.Locale
;
import
java.util.Scanner
;
public
class
DodajProdukt
{
public
static
void
main
(
String
[]
args
)
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
scanner
.
useLocale
(
Locale
.
US
);
final
String
url
=
"jdbc:postgresql://localhost:5432/sklep"
;
final
String
user
=
"kurs"
;
final
String
password
=
"abc123"
;
try
(
Connection
c
=
DriverManager
.
getConnection
(
url
,
user
,
password
))
{
c
.
setAutoCommit
(
false
);
while
(
true
)
{
System
.
out
.
print
(
"Podaj nazwę nowego produktu (pusty napis, aby zakończyć): "
);
String
name
=
scanner
.
nextLine
();
if
(
name
.
isEmpty
())
break
;
System
.
out
.
print
(
"Podaj cenę: "
);
BigDecimal
price
=
scanner
.
nextBigDecimal
();
scanner
.
nextLine
();
System
.
out
.
print
(
"Podaj stawkę VAT, np 23 : "
);
int
vat
=
scanner
.
nextInt
();
scanner
.
nextLine
();
System
.
out
.
print
(
"Podaj opis: "
);
String
description
=
scanner
.
nextLine
();
if
(
description
.
isEmpty
())
{
description
=
null
;
}
// aby wpisać rekord do bazy, tworzymy obiekt bez określonego ID
final
String
sql
=
"INSERT INTO products(product_name, price, vat, description) VALUES(?, ?, ?, ?)"
;
final
String
[]
kolumnyID
=
{
"product_id"
};
try
(
PreparedStatement
stmt
=
c
.
prepareStatement
(
sql
,
kolumnyID
))
{
stmt
.
setString
(
1
,
name
);
stmt
.
setBigDecimal
(
2
,
price
);
stmt
.
setBigDecimal
(
3
,
BigDecimal
.
valueOf
(
vat
).
movePointLeft
(
2
));
stmt
.
setString
(
4
,
description
);
stmt
.
executeUpdate
();
// teraz mogę odczytać wygenerowane ID
try
(
ResultSet
rs
=
stmt
.
getGeneratedKeys
())
{
if
(
rs
.
next
())
{
int
id
=
rs
.
getInt
(
1
);
System
.
out
.
println
(
"Nowy produkt uzyskał numer "
+
id
);
}
}
}
}
System
.
out
.
println
(
"Czy zapisać zmiany? [T/N]"
);
String
wybor
=
scanner
.
next
().
toUpperCase
();
switch
(
wybor
)
{
case
"T"
:
System
.
out
.
println
(
"Zatwierdzam transakcję"
);
c
.
commit
();
break
;
case
"N"
:
System
.
out
.
println
(
"Cofam transakcję"
);
c
.
rollback
();
break
;
default
:
System
.
out
.
println
(
"Rozłączam się bez zatwierdzenia transakcji"
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
PC25-SklepWeb/src/main/java/dostep_podstawowy/WypiszProdukty.java
0 → 100644
View file @
f4a93912
package
dostep_podstawowy
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
public
class
WypiszProdukty
{
public
static
void
main
(
String
[]
args
)
{
final
String
url
=
"jdbc:postgresql://localhost:5432/sklep"
;
final
String
user
=
"kurs"
;
final
String
password
=
"abc123"
;
final
String
sql
=
"SELECT * FROM products ORDER BY product_id"
;
try
(
Connection
c
=
DriverManager
.
getConnection
(
url
,
user
,
password
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
sql
);
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
System
.
out
.
printf
(
"%3d: %s, cena %.2f, vat %.0f%%, opis: %s\n"
,
rs
.
getInt
(
"product_id"
),
rs
.
getString
(
"product_name"
),
rs
.
getBigDecimal
(
"price"
),
rs
.
getBigDecimal
(
"vat"
).
movePointRight
(
2
),
// jeśli nie null
rs
.
getString
(
"description"
));
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
};
}
}
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