Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
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
20240528-BJava
Commits
a3fadfea
Commit
a3fadfea
authored
Jun 25, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dodatkowy przykład dot. bazy danych
parent
10082fd5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
0 deletions
+51
-0
P07_GenerowaneId.java
...ych/src/main/java/gotowe/postgresql/P07_GenerowaneId.java
+51
-0
No files found.
PC22-BazyDanych/src/main/java/gotowe/postgresql/P07_GenerowaneId.java
0 → 100644
View file @
a3fadfea
package
gotowe
.
postgresql
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.time.LocalTime
;
import
java.time.format.DateTimeFormatter
;
public
class
P07_GenerowaneId
{
/* W abzach danych często stosowane są automatycznie generowane klucze. Gdy wykomnujemy polecenie INSERT nie wymieniając kolumny z kluczem,
* jego wartość jest generowana automatycznie (z sekwencji lub w oparciu o specjalny typ kolumny).
* Ale aplikacja zwykle potrzebuje poznać to ID, aby móc odwoływać się do tego rekordu w dalszej części programu.
* JDBC oferuje mechanizm, w którym przed wykonaniem operacji INSERT w obiekcie Statement rejestruje się automatycznie generowane kolumny,
* a po wykonaniu polecenia można pobrać ich wartości (jako ResultSet).
* W praktyce chodzi o takie kolumny, jak autoinkrementowane ID (przede wszystkim), czas utworzenia rekordu, czasami inne wartości domyślne.
* Tu dodatkowo używam state_province, co jest o tyle nieciekawe, że tam po prostu będzie null,
* ale po prostu pokazuję, że tych pól można podać więcej niż jedno.
*/
public
static
void
main
(
String
[]
args
)
{
// Tablica nazw kolumn, których wartości są wstawiane automatycznie.
// Zamiast nazw mogą być też numery (wtedy tablica intów).
String
[]
polaGenerowane
=
{
"location_id"
,
"state_province"
};
String
sql
=
"INSERT INTO locations(street_address, postal_code, city, country_id) VALUES(?, ?, ?, ?)"
;
try
(
Connection
c
=
DriverManager
.
getConnection
(
Ustawienia
.
URL
,
Ustawienia
.
USER
,
Ustawienia
.
PASSWD
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
sql
,
polaGenerowane
))
{
stmt
.
setString
(
1
,
"Nowa z "
+
LocalTime
.
now
().
format
(
DateTimeFormatter
.
ofPattern
(
"HH:mm:SS"
)));
stmt
.
setString
(
2
,
"01-321"
);
stmt
.
setString
(
3
,
"Warszawa"
);
stmt
.
setString
(
4
,
"US"
);
int
ile
=
stmt
.
executeUpdate
();
System
.
out
.
println
(
"Wstawiono "
+
ile
+
" rekord"
);
// powinno być 1
// odcyt wartości wygenerowanych pól
try
(
ResultSet
rsGen
=
stmt
.
getGeneratedKeys
())
{
if
(
rsGen
.
next
())
{
int
id
=
rsGen
.
getInt
(
1
);
String
state
=
rsGen
.
getString
(
2
);
System
.
out
.
println
(
"wygenerowane wartości pól: "
+
id
+
", "
+
state
);
}
}
}
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