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
d065e5e2
Commit
d065e5e2
authored
Apr 05, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Eksport do CSV za pomocą Apache Commons CSV
parent
41eb662a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
0 deletions
+48
-0
pom.xml
PC23-BazyDanych_Maven/pom.xml
+5
-0
EksportCSV.java
...BazyDanych_Maven/src/main/java/biblioteki/EksportCSV.java
+43
-0
No files found.
PC23-BazyDanych_Maven/pom.xml
View file @
d065e5e2
...
...
@@ -26,6 +26,11 @@
<version>
3.41.2.1
</version>
<scope>
runtime
</scope>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-csv
</artifactId>
<version>
1.10.0
</version>
</dependency>
</dependencies>
</project>
...
...
PC23-BazyDanych_Maven/src/main/java/biblioteki/EksportCSV.java
0 → 100644
View file @
d065e5e2
package
biblioteki
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
org.apache.commons.csv.CSVFormat
;
import
org.apache.commons.csv.CSVPrinter
;
public
class
EksportCSV
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Zadaję zapytanie"
);
try
(
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/hr"
,
"kurs"
,
"abc123"
);
PreparedStatement
stmt
=
c
.
prepareStatement
(
"SELECT * FROM employees"
);
ResultSet
rs
=
stmt
.
executeQuery
())
{
System
.
out
.
println
(
"Zapisuję do pliku"
);
eksportujDoCSV
(
rs
,
"eksport.csv"
);
System
.
out
.
println
(
"Gotowe"
);
}
catch
(
SQLException
|
IOException
e
)
{
e
.
printStackTrace
();
}
}
static
void
eksportujDoCSV
(
ResultSet
rs
,
String
sciezka
)
throws
SQLException
,
IOException
{
CSVFormat
format
=
CSVFormat
.
Builder
.
create
()
.
setDelimiter
(
';'
)
.
setHeader
(
rs
)
.
build
();
try
(
PrintWriter
out
=
new
PrintWriter
(
sciezka
);
CSVPrinter
printer
=
new
CSVPrinter
(
out
,
format
))
{
printer
.
printRecords
(
rs
);
}
}
}
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