Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
798deef2
Commit
798deef2
authored
Apr 04, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady bazodanowe w wersji Maven
parent
d8dbae19
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
330 additions
and
0 deletions
+330
-0
pom.xml
PC23-BazyDanych_Maven/pom.xml
+18
-0
Parametry1.java
...yDanych_Maven/src/main/java/bazy/podstawy/Parametry1.java
+47
-0
Parametry2.java
...yDanych_Maven/src/main/java/bazy/podstawy/Parametry2.java
+45
-0
Parametry3.java
...yDanych_Maven/src/main/java/bazy/podstawy/Parametry3.java
+42
-0
Podwyzka1.java
...zyDanych_Maven/src/main/java/bazy/podstawy/Podwyzka1.java
+34
-0
Podwyzka2.java
...zyDanych_Maven/src/main/java/bazy/podstawy/Podwyzka2.java
+69
-0
ProsteZapytanie1.java
...h_Maven/src/main/java/bazy/podstawy/ProsteZapytanie1.java
+44
-0
ProsteZapytanie2.java
...h_Maven/src/main/java/bazy/podstawy/ProsteZapytanie2.java
+31
-0
No files found.
PC23-BazyDanych_Maven/pom.xml
View file @
798deef2
...
...
@@ -5,4 +5,22 @@
<groupId>
pl.alx.kjava
</groupId>
<artifactId>
PC23-BazyDanych_Maven
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<properties>
<maven.compiler.source>
17
</maven.compiler.source>
<maven.compiler.target>
17
</maven.compiler.target>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<!-- po zmianie podstawowych ustawień w Eclipse robimy Alt+F5 -->
</properties>
<dependencies>
<dependency>
<groupId>
org.postgresql
</groupId>
<artifactId>
postgresql
</artifactId>
<version>
42.6.0
</version>
<scope>
runtime
</scope>
</dependency>
</dependencies>
</project>
PC23-BazyDanych_Maven/src/main/java/bazy/podstawy/Parametry1.java
0 → 100644
View file @
798deef2
package
bazy
.
podstawy
;
import
java.math.BigDecimal
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.Objects
;
import
javax.swing.JOptionPane
;
public
class
Parametry1
{
/* Rozwiązanie 1:
Z bazy odczytujemy wszystkie rekordy, a po stronie w Javy, za pomocą equals filtrujemy rekordy i wyświeltlamy tylko te, które spełniają warunek.
Problem: niska wydajność.
Baza musi odczytać wszystkie rekordy z dysku (nawet, gdyby były zdefinioane indeksy),
wszystkie dane są transferowane przez sieć,
wszystkie dane są przeglądane przez Javę, co obciąża aplikację kliencką.
*/
public
static
void
main
(
String
[]
args
)
{
String
szukany_job
=
JOptionPane
.
showInputDialog
(
"Podaj kod stanowiska"
);
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/hr"
,
"kurs"
,
"abc123"
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees"
);
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
int
id
=
rs
.
getInt
(
1
);
String
imie
=
rs
.
getString
(
"first_name"
);
String
nazwisko
=
rs
.
getString
(
"last_name"
);
String
job
=
rs
.
getString
(
"job_id"
);
BigDecimal
pensja
=
rs
.
getBigDecimal
(
"salary"
);
java
.
sql
.
Date
data
=
rs
.
getDate
(
"hire_date"
);
if
(
Objects
.
equals
(
job
,
szukany_job
))
{
System
.
out
.
printf
(
"%d. %s %s (%s) zatr. %s zarabia %s\n"
,
id
,
imie
,
nazwisko
,
job
,
data
,
pensja
);
}
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
PC23-BazyDanych_Maven/src/main/java/bazy/podstawy/Parametry2.java
0 → 100644
View file @
798deef2
package
bazy
.
podstawy
;
import
java.math.BigDecimal
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.Objects
;
import
javax.swing.JOptionPane
;
public
class
Parametry2
{
/* Rozwiązanie 2:
* Za pomocą WHERE filtrujemy rekordy po stronie bazy danych, co poprawia wydajność.
*
* Ze względu na to, że zapytaniu budujemy samodzielnie sklejając kawałki tekstu, stwarzamy zagrożenie SQL Injection.
* Przykładowe dane powodujące SQL injection:
* a';UPDATE employees SET salary = 50000 WHERE last_name='Hunold'; SELECT '
* a'; DROP TABLE employees CASCADE; SELECT '
*/
public
static
void
main
(
String
[]
args
)
{
String
szukany_job
=
JOptionPane
.
showInputDialog
(
"Podaj kod stanowiska"
);
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/hr"
,
"kurs"
,
"abc123"
))
{
try
(
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees WHERE job_id = '"
+
szukany_job
+
"'"
))
{
System
.
out
.
println
(
"Zaraz wykonam: "
+
stmt
);
try
(
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
int
id
=
rs
.
getInt
(
1
);
String
imie
=
rs
.
getString
(
"first_name"
);
String
nazwisko
=
rs
.
getString
(
"last_name"
);
String
job
=
rs
.
getString
(
"job_id"
);
BigDecimal
pensja
=
rs
.
getBigDecimal
(
"sal-ary"
);
java
.
sql
.
Date
data
=
rs
.
getDate
(
"hire_date"
);
System
.
out
.
printf
(
"%d. %s %s (%s) zatr. %s zarabia %s\n"
,
id
,
imie
,
nazwisko
,
job
,
data
,
pensja
);
}
}
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
PC23-BazyDanych_Maven/src/main/java/bazy/podstawy/Parametry3.java
0 → 100644
View file @
798deef2
package
bazy
.
podstawy
;
import
java.math.BigDecimal
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.Objects
;
import
javax.swing.JOptionPane
;
public
class
Parametry3
{
/* Rozwiązanie 3:
* Za pomocą WHERE filtrujemy rekordy po stronie bazy danych, co poprawia wydajność.
* Tutaj prawidłowo używamy parametrów JDBC zapisywanych za pomocą ?
*/
public
static
void
main
(
String
[]
args
)
{
String
szukany_job
=
JOptionPane
.
showInputDialog
(
"Podaj kod stanowiska"
);
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/hr"
,
"kurs"
,
"abc123"
))
{
try
(
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees WHERE job_id = ?"
))
{
stmt
.
setString
(
1
,
szukany_job
);
System
.
out
.
println
(
"Zaraz wykonam: "
+
stmt
);
try
(
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
int
id
=
rs
.
getInt
(
1
);
String
imie
=
rs
.
getString
(
"first_name"
);
String
nazwisko
=
rs
.
getString
(
"last_name"
);
String
job
=
rs
.
getString
(
"job_id"
);
BigDecimal
pensja
=
rs
.
getBigDecimal
(
"salary"
);
java
.
sql
.
Date
data
=
rs
.
getDate
(
"hire_date"
);
System
.
out
.
printf
(
"%d. %s %s (%s) zatr. %s zarabia %s\n"
,
id
,
imie
,
nazwisko
,
job
,
data
,
pensja
);
}
}
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
PC23-BazyDanych_Maven/src/main/java/bazy/podstawy/Podwyzka1.java
0 → 100644
View file @
798deef2
package
bazy
.
podstawy
;
import
java.math.BigDecimal
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.SQLException
;
import
java.util.Scanner
;
public
class
Podwyzka1
{
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/hr"
,
"kurs"
,
"abc123"
))
{
// połączenie inicjalnie jest w trybie "auto commit", czyli każda zmiana jest od razu zapisywana w sposób trwały w bazie danych
System
.
out
.
print
(
"Podaj job_id: "
);
String
job
=
sc
.
nextLine
();
System
.
out
.
print
(
"Podaj kwotę podwyżki: "
);
BigDecimal
kwota
=
sc
.
nextBigDecimal
();
final
String
sql
=
"UPDATE employees SET salary = salary + ? WHERE job_id = ?"
;
try
(
PreparedStatement
stmt
=
c
.
prepareStatement
(
sql
))
{
stmt
.
setBigDecimal
(
1
,
kwota
);
stmt
.
setString
(
2
,
job
);
int
ile
=
stmt
.
executeUpdate
();
System
.
out
.
println
(
"Zmodyfikowano "
+
ile
+
" rekordów."
);
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
PC23-BazyDanych_Maven/src/main/java/bazy/podstawy/Podwyzka2.java
0 → 100644
View file @
798deef2
package
bazy
.
podstawy
;
import
java.math.BigDecimal
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.Scanner
;
public
class
Podwyzka2
{
public
static
void
main
(
String
[]
args
)
{
final
String
url
=
"jdbc:postgresql://localhost/hr"
;
final
String
sqlUpdate
=
"UPDATE employees SET salary = salary + ? WHERE job_id = ?"
;
final
String
sqlAvg
=
"SELECT avg(salary) FROM employees WHERE job_id = ?"
;
Scanner
sc
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Podaj job_id: "
);
String
job
=
sc
.
nextLine
();
try
(
Connection
c
=
DriverManager
.
getConnection
(
url
,
"kurs"
,
"abc123"
))
{
c
.
setAutoCommit
(
false
);
try
(
PreparedStatement
stmtAvg
=
c
.
prepareStatement
(
sqlAvg
))
{
stmtAvg
.
setString
(
1
,
job
);
try
(
ResultSet
rs
=
stmtAvg
.
executeQuery
())
{
if
(
rs
.
next
())
{
double
srednia
=
rs
.
getDouble
(
1
);
System
.
out
.
printf
(
"Średnia pensja: %.2f\n"
,
srednia
);
}
}
System
.
out
.
print
(
"Podaj kwotę podwyżki: "
);
BigDecimal
kwota
=
sc
.
nextBigDecimal
();
try
(
PreparedStatement
stmt
=
c
.
prepareStatement
(
sqlUpdate
))
{
stmt
.
setBigDecimal
(
1
,
kwota
);
stmt
.
setString
(
2
,
job
);
int
ile
=
stmt
.
executeUpdate
();
System
.
out
.
println
(
"Zmodyfikowano "
+
ile
+
" rekordów."
);
}
try
(
ResultSet
rs
=
stmtAvg
.
executeQuery
())
{
if
(
rs
.
next
())
{
double
srednia
=
rs
.
getDouble
(
1
);
System
.
out
.
printf
(
"Średnia pensja: %.2f\n"
,
srednia
);
}
}
System
.
out
.
print
(
"Czy zatwierdzić transakcję? T / N: "
);
String
wybor
=
sc
.
next
().
toUpperCase
();
switch
(
wybor
)
{
case
"T"
->
c
.
commit
();
case
"N"
->
c
.
rollback
();
}
try
(
ResultSet
rs
=
stmtAvg
.
executeQuery
())
{
if
(
rs
.
next
())
{
double
srednia
=
rs
.
getDouble
(
1
);
System
.
out
.
printf
(
"Średnia pensja: %.2f\n"
,
srednia
);
}
}
System
.
out
.
println
(
"Koniec"
);
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
PC23-BazyDanych_Maven/src/main/java/bazy/podstawy/ProsteZapytanie1.java
0 → 100644
View file @
798deef2
package
bazy
.
podstawy
;
import
java.math.BigDecimal
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
public
class
ProsteZapytanie1
{
// JDBC - Java Database Connectivity
// Programista w swoim kodzie używa ogólnych interfejsów i operacji:
// Connection, PreparedStatement, ResultSet, executeQuery...
// natomiast ich implementacje są dostarczane w formie "sterowników" właściwych dla poszczególnych rodzajów baz danych.
public
static
void
main
(
String
[]
args
)
{
try
{
// Koncepcyjnie rzecz biorąc, robimy coś takiego, ale zapisuje się to ogólnie...
// Connection c = new PGConnection(...);
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/hr"
,
"kurs"
,
"abc123"
);
System
.
out
.
println
(
"Udało się połączyć. Połączenie: "
+
c
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees"
);
ResultSet
rs
=
stmt
.
executeQuery
();
System
.
out
.
println
(
"Mam wyniki: "
+
rs
);
while
(
rs
.
next
())
{
int
id
=
rs
.
getInt
(
1
);
// kolumna wynikowa nr 1 - numeracja od 1!
String
imie
=
rs
.
getString
(
2
);
// kolumna nr 2
// ale wartości można też odczytywać podając nazwę kolumny:
String
nazwisko
=
rs
.
getString
(
"last_name"
);
String
job
=
rs
.
getString
(
"job_id"
);
BigDecimal
pensja
=
rs
.
getBigDecimal
(
"salary"
);
java
.
sql
.
Date
data
=
rs
.
getDate
(
"hire_date"
);
System
.
out
.
printf
(
"%d. %s %s (%s) zatr. %s zarabia %s\n"
,
id
,
imie
,
nazwisko
,
job
,
data
,
pensja
);
}
c
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
PC23-BazyDanych_Maven/src/main/java/bazy/podstawy/ProsteZapytanie2.java
0 → 100644
View file @
798deef2
package
bazy
.
podstawy
;
import
java.math.BigDecimal
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
public
class
ProsteZapytanie2
{
public
static
void
main
(
String
[]
args
)
{
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/hr"
,
"kurs"
,
"abc123"
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees"
);
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
int
id
=
rs
.
getInt
(
1
);
String
imie
=
rs
.
getString
(
"first_name"
);
String
nazwisko
=
rs
.
getString
(
"last_name"
);
String
job
=
rs
.
getString
(
"job_id"
);
BigDecimal
pensja
=
rs
.
getBigDecimal
(
"salary"
);
java
.
sql
.
Date
data
=
rs
.
getDate
(
"hire_date"
);
System
.
out
.
printf
(
"%d. %s %s (%s) zatr. %s zarabia %s\n"
,
id
,
imie
,
nazwisko
,
job
,
data
,
pensja
);
}
}
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