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
665bdd32
Commit
665bdd32
authored
Apr 04, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przekazywanie parametrów
parent
c05bc5d8
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
166 additions
and
2 deletions
+166
-2
Parametry1.java
PC22-BazyDanych_Wstep/src/bazy/podstawy/Parametry1.java
+47
-0
Parametry2.java
PC22-BazyDanych_Wstep/src/bazy/podstawy/Parametry2.java
+45
-0
Parametry3.java
PC22-BazyDanych_Wstep/src/bazy/podstawy/Parametry3.java
+42
-0
ProsteZapytanie1.java
...-BazyDanych_Wstep/src/bazy/podstawy/ProsteZapytanie1.java
+1
-2
ProsteZapytanie2.java
...-BazyDanych_Wstep/src/bazy/podstawy/ProsteZapytanie2.java
+31
-0
No files found.
PC22-BazyDanych_Wstep/src/bazy/podstawy/Parametry1.java
0 → 100644
View file @
665bdd32
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
();
}
}
}
PC22-BazyDanych_Wstep/src/bazy/podstawy/Parametry2.java
0 → 100644
View file @
665bdd32
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
();
}
}
}
PC22-BazyDanych_Wstep/src/bazy/podstawy/Parametry3.java
0 → 100644
View file @
665bdd32
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
();
}
}
}
PC22-BazyDanych_Wstep/src/bazy/podstawy/ProsteZapytanie1.java
View file @
665bdd32
...
...
@@ -34,12 +34,11 @@ public class ProsteZapytanie1 {
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
();
}
}
}
PC22-BazyDanych_Wstep/src/bazy/podstawy/ProsteZapytanie2.java
0 → 100644
View file @
665bdd32
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