Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
8a842c9e
Commit
8a842c9e
authored
5 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Cosmetics
parent
8506af41
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Klausuren/Sda1/2018Winter/Solve/src/main/java/de/hdm_stuttgart/mi/sda1/Book2Rdbms.java
+35
-18
35 additions, 18 deletions
...ve/src/main/java/de/hdm_stuttgart/mi/sda1/Book2Rdbms.java
with
35 additions
and
18 deletions
Klausuren/Sda1/2018Winter/Solve/src/main/java/de/hdm_stuttgart/mi/sda1/Book2Rdbms.java
+
35
−
18
View file @
8a842c9e
...
...
@@ -12,6 +12,15 @@ import java.sql.SQLException;
public
class
Book2Rdbms
implements
AutoCloseable
{
static
private
final
String
insertSectionSql
=
"INSERT INTO Section (id, title, parent, revisionflag)"
+
" VALUES (?, ?, ?, ?);"
,
insertParaSql
=
"INSERT INTO Para (parent, para, revisionflag) \n"
+
"VALUES ( ?, ?, ?);"
;
private
Connection
conn
;
private
PreparedStatement
insertSection
,
insertPara
;
...
...
@@ -22,11 +31,9 @@ public class Book2Rdbms implements AutoCloseable {
try
{
conn
=
DriverManager
.
getConnection
(
"jdbc:postgresql://localhost/hdm"
,
"hdmuser"
,
"XYZ"
);
insertSection
=
conn
.
prepareStatement
(
"INSERT INTO Section (id, title, parent, revisionFlag) VALUES (?, ?, ?, ?);"
);
insertSection
=
conn
.
prepareStatement
(
insertSectionSql
);
insertPara
=
conn
.
prepareStatement
(
"INSERT INTO Para (section, para, revisionFlag) VALUES (?, ?, ?)"
);
insertPara
=
conn
.
prepareStatement
(
insertParaSql
);
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Unable to establish connection: "
+
e
);
...
...
@@ -55,6 +62,7 @@ public class Book2Rdbms implements AutoCloseable {
private
void
handleSection
(
final
String
parentId
,
final
Element
section
)
{
try
{
final
String
currentSectionId
=
section
.
getAttributeValue
(
"id"
);
insertSection
.
setString
(
1
,
currentSectionId
);
insertSection
.
setString
(
2
,
section
.
getChildText
(
"title"
));
insertSection
.
setString
(
3
,
parentId
);
...
...
@@ -62,19 +70,9 @@ public class Book2Rdbms implements AutoCloseable {
insertSection
.
execute
();
section
.
getChildren
(
"para"
).
forEach
(
p
->
{
try
{
insertPara
.
setString
(
1
,
currentSectionId
);
insertPara
.
setString
(
2
,
p
.
getValue
());
insertPara
.
setString
(
3
,
p
.
getAttributeValue
(
"revisionflag"
));
insertPara
.
execute
();
}
catch
(
final
SQLException
e
)
{
e
.
printStackTrace
();
System
.
err
.
println
(
"Unable to insert <para>: "
+
e
);
System
.
exit
(
1
);
}
}
);
// add <para> children of current section
handleParaChildren
(
section
);
// recurse to <section> children
section
.
getChildren
(
"section"
).
forEach
(
s
->
handleSection
(
currentSectionId
,
s
));
}
catch
(
final
SQLException
e
)
{
...
...
@@ -84,8 +82,27 @@ public class Book2Rdbms implements AutoCloseable {
}
}
public
void
handleParaChildren
(
final
Element
section
)
{
final
String
currentSectionId
=
section
.
getAttributeValue
(
"id"
);
section
.
getChildren
(
"para"
).
forEach
(
p
->
{
try
{
insertPara
.
setString
(
1
,
currentSectionId
);
insertPara
.
setString
(
2
,
p
.
getValue
());
insertPara
.
setString
(
3
,
p
.
getAttributeValue
(
"revisionflag"
));
insertPara
.
execute
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
);
}
@Override
public
void
close
(){
public
void
close
()
{
try
{
conn
.
close
();
}
catch
(
final
SQLException
e
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment