Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
AplikacjaALX
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
AplikacjaALX
Commits
bf816ee6
Commit
bf816ee6
authored
May 10, 2024
by
patryk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
przykłady JDBC
parent
4c0315d7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
0 deletions
+103
-0
pom.xml
pom.xml
+7
-0
Grupowanie.java
src/main/java/sqlite/Grupowanie.java
+36
-0
Odczyt1.java
src/main/java/sqlite/Odczyt1.java
+33
-0
Odczyt2.java
src/main/java/sqlite/Odczyt2.java
+27
-0
No files found.
pom.xml
View file @
bf816ee6
...
@@ -50,6 +50,12 @@
...
@@ -50,6 +50,12 @@
<version>
3.25.3
</version>
<version>
3.25.3
</version>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
org.xerial
</groupId>
<artifactId>
sqlite-jdbc
</artifactId>
<version>
3.45.3.0
</version>
<scope>
runtime
</scope>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
\ No newline at end of file
src/main/java/sqlite/Grupowanie.java
0 → 100644
View file @
bf816ee6
package
sqlite
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
public
class
Grupowanie
{
public
static
void
main
(
String
[]
args
)
{
final
String
sql
=
"""
SELECT job_id, job_title, count(employee_id), avg(salary), min(salary), max(salary)
FROM jobs LEFT JOIN employees USING(job_id)
GROUP BY job_id, job_title
ORDER BY 3 DESC, 2;
"""
;
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:sqlite:hr.db"
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
sql
);
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
// odczyt wg numerów kolumn, numeracja od 1
System
.
out
.
printf
(
"|%-10s| %-32s | %2d | %8.2f | %8.2f | %8.2f |%n"
,
rs
.
getString
(
1
),
rs
.
getString
(
2
),
rs
.
getInt
(
3
),
rs
.
getBigDecimal
(
4
),
rs
.
getBigDecimal
(
5
),
rs
.
getBigDecimal
(
6
));
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/sqlite/Odczyt1.java
0 → 100644
View file @
bf816ee6
package
sqlite
;
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
Odczyt1
{
public
static
void
main
(
String
[]
args
)
{
try
{
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:sqlite:hr.db"
);
System
.
out
.
println
(
"Udało się połączyć. c = "
+
c
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees"
);
ResultSet
rs
=
stmt
.
executeQuery
();
System
.
out
.
println
(
"Mam wyniki: "
+
rs
);
while
(
rs
.
next
())
{
String
firstName
=
rs
.
getString
(
"first_name"
);
String
lastName
=
rs
.
getString
(
"last_name"
);
BigDecimal
salary
=
rs
.
getBigDecimal
(
"salary"
);
System
.
out
.
println
(
firstName
+
" "
+
lastName
+
" zarabia "
+
salary
);
}
rs
.
close
();
stmt
.
close
();
c
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/sqlite/Odczyt2.java
0 → 100644
View file @
bf816ee6
package
sqlite
;
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
Odczyt2
{
public
static
void
main
(
String
[]
args
)
{
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:sqlite:hr.db"
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees"
);
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
String
firstName
=
rs
.
getString
(
"first_name"
);
String
lastName
=
rs
.
getString
(
"last_name"
);
BigDecimal
salary
=
rs
.
getBigDecimal
(
"salary"
);
System
.
out
.
println
(
firstName
+
" "
+
lastName
+
" zarabia "
+
salary
);
}
}
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