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
bc62e7e5
Commit
bc62e7e5
authored
Apr 04, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Programy "podwyżka", transakcje
parent
665bdd32
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
0 deletions
+103
-0
Podwyzka1.java
PC22-BazyDanych_Wstep/src/bazy/podstawy/Podwyzka1.java
+34
-0
Podwyzka2.java
PC22-BazyDanych_Wstep/src/bazy/podstawy/Podwyzka2.java
+69
-0
No files found.
PC22-BazyDanych_Wstep/src/bazy/podstawy/Podwyzka1.java
0 → 100644
View file @
bc62e7e5
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
();
}
}
}
PC22-BazyDanych_Wstep/src/bazy/podstawy/Podwyzka2.java
0 → 100644
View file @
bc62e7e5
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
();
}
}
}
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