Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Ondřej Skoumal
TrainingDiary
Commits
e504c084
Commit
e504c084
authored
Apr 21, 2014
by
Ondra
Browse files
Edit Repozitaru
parent
7275a0fa
Changes
8
Hide whitespace changes
Inline
Side-by-side
src/cz/cvut/fel/skoumond/entities/BaseEntity.java
View file @
e504c084
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
cz.cvut.fel.skoumond.entities
;
/**
...
...
src/cz/cvut/fel/skoumond/entities/TypeEntity.java
View file @
e504c084
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
cz.cvut.fel.skoumond.entities
;
import
java.util.Objects
;
...
...
src/cz/cvut/fel/skoumond/exceptions/DataEntryException.java
0 → 100644
View file @
e504c084
package
cz.cvut.fel.skoumond.exceptions
;
/**
*
* @author Ondra
*/
public
class
DataEntryException
extends
RuntimeException
{
public
DataEntryException
()
{
super
();
}
public
DataEntryException
(
String
string
)
{
super
(
string
);
}
public
DataEntryException
(
String
string
,
Throwable
thrwbl
)
{
super
(
string
,
thrwbl
);
}
public
DataEntryException
(
Throwable
thrwbl
)
{
super
(
thrwbl
);
}
protected
DataEntryException
(
String
string
,
Throwable
thrwbl
,
boolean
bln
,
boolean
bln1
)
{
super
(
string
,
thrwbl
,
bln
,
bln1
);
}
}
src/cz/cvut/fel/skoumond/exceptions/NoConnectionException.java
View file @
e504c084
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
cz.cvut.fel.skoumond.exceptions
;
/**
...
...
src/cz/cvut/fel/skoumond/sql/IRepository.java
View file @
e504c084
...
...
@@ -4,14 +4,13 @@
*/
package
cz.cvut.fel.skoumond.sql
;
import
cz.cvut.fel.skoumond.entities.BaseEntity
;
import
java.util.List
;
/**
*
* @author Ondra
*/
public
interface
IRepository
<
T
extends
BaseEntity
>
{
public
interface
IRepository
<
T
>
{
public
T
findById
(
int
index
);
...
...
src/cz/cvut/fel/skoumond/sql/
SQLManager
.java
→
src/cz/cvut/fel/skoumond/sql/
Repository
.java
View file @
e504c084
...
...
@@ -4,6 +4,7 @@
*/
package
cz.cvut.fel.skoumond.sql
;
import
cz.cvut.fel.skoumond.entities.BaseEntity
;
import
cz.cvut.fel.skoumond.exceptions.NoConnectionException
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
...
...
@@ -13,28 +14,22 @@ import java.sql.SQLException;
*
* @author Ondra
*/
public
class
SQLManager
{
public
abstract
class
Repository
<
T
extends
BaseEntity
>
implements
IRepository
<
T
>
{
protected
static
Connection
getConnection
()
{
private
SQLManager
()
{
}
public
static
Connection
getConnection
()
{
try
{
// JDBC driver
Class
.
forName
(
"org.apache.derby.jdbc.EmbeddedDriver"
);
}
catch
(
java
.
lang
.
ClassNotFoundException
e
)
{
}
catch
(
java
.
lang
.
ClassNotFoundException
e
)
{
System
.
err
.
println
(
"Chyba při načtení databázového ovladače: "
+
e
.
getMessage
());
}
try
(
Connection
connection
=
DriverManager
.
getConnection
(
"jdbc:derby:database;create=true"
,
"skoumond"
,
"heslo"
))
{
return
connection
;
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Chyba při komunikaci s databází"
+
e
.
getMessage
());
try
{
return
DriverManager
.
getConnection
(
"jdbc:derby:database;create=true"
,
"skoumond"
,
"heslo"
);
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Chyba při komunikaci s databází: "
+
e
.
getMessage
());
throw
new
NoConnectionException
();
}
}
}
}
}
src/cz/cvut/fel/skoumond/sql/RunRepository.java
0 → 100644
View file @
e504c084
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
cz.cvut.fel.skoumond.sql
;
import
cz.cvut.fel.skoumond.entities.RunEntity
;
import
java.util.List
;
/**
*
* @author Ondra
*/
public
class
RunRepository
extends
Repository
<
RunEntity
>
{
@Override
public
RunEntity
findById
(
int
index
)
{
throw
new
UnsupportedOperationException
(
"Not supported yet."
);
//To change body of generated methods, choose Tools | Templates.
}
@Override
public
List
<
RunEntity
>
all
()
{
throw
new
UnsupportedOperationException
(
"Not supported yet."
);
//To change body of generated methods, choose Tools | Templates.
}
@Override
public
void
save
(
RunEntity
entity
)
{
throw
new
UnsupportedOperationException
(
"Not supported yet."
);
//To change body of generated methods, choose Tools | Templates.
}
@Override
public
void
update
(
RunEntity
entity
)
{
throw
new
UnsupportedOperationException
(
"Not supported yet."
);
//To change body of generated methods, choose Tools | Templates.
}
@Override
public
void
delete
(
RunEntity
entity
)
{
throw
new
UnsupportedOperationException
(
"Not supported yet."
);
//To change body of generated methods, choose Tools | Templates.
}
}
src/cz/cvut/fel/skoumond/sql/TypeRepository.java
0 → 100644
View file @
e504c084
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
cz.cvut.fel.skoumond.sql
;
import
cz.cvut.fel.skoumond.entities.TypeEntity
;
import
cz.cvut.fel.skoumond.exceptions.DataEntryException
;
import
cz.cvut.fel.skoumond.exceptions.NoConnectionException
;
import
static
cz
.
cvut
.
fel
.
skoumond
.
sql
.
Repository
.
getConnection
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
*
* @author Ondra
*/
public
class
TypeRepository
extends
Repository
<
TypeEntity
>
{
private
final
String
FINDBYID
=
"SELECT * FROM APP.TYPE WHERE T_ID = ?"
;
private
final
String
ALL
=
"SELECT * APP.TYPE"
;
private
final
String
INSERT
=
"INSERT INTO APP.TYPE VALUES (?, ?)"
;
private
final
String
UPDATE
=
"UPDATE APP.TYPE SET T_TYPE = ? WHERE T_ID = ?"
;
private
final
String
DELETE
=
"DELETE FROM APP.TYPE WHERE T_ID = ?"
;
@Override
public
TypeEntity
findById
(
int
index
)
throws
NoConnectionException
{
try
(
Connection
con
=
getConnection
();
PreparedStatement
ps
=
con
.
prepareStatement
(
FINDBYID
))
{
ps
.
setInt
(
1
,
index
);
ResultSet
rs
=
ps
.
executeQuery
();
rs
.
next
();
return
new
TypeEntity
(
rs
.
getInt
(
1
),
rs
.
getString
(
2
));
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Chyba při komunikaci s databází: "
+
e
.
getMessage
());
throw
new
NoConnectionException
();
}
}
@Override
public
List
<
TypeEntity
>
all
()
throws
NoConnectionException
{
List
<
TypeEntity
>
arrayList
=
new
ArrayList
();
try
(
Connection
con
=
getConnection
();
PreparedStatement
ps
=
con
.
prepareStatement
(
ALL
))
{
ResultSet
rs
=
ps
.
executeQuery
();
while
(
rs
.
next
())
{
arrayList
.
add
(
new
TypeEntity
(
rs
.
getInt
(
1
),
rs
.
getString
(
2
)));
}
return
arrayList
;
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Chyba při komunikaci s databází: "
+
e
.
getMessage
());
throw
new
NoConnectionException
();
}
}
@Override
public
void
save
(
TypeEntity
entity
)
throws
NoConnectionException
{
try
(
Connection
con
=
getConnection
();
PreparedStatement
ps
=
con
.
prepareStatement
(
INSERT
))
{
ps
.
setInt
(
1
,
entity
.
getId
());
ps
.
setString
(
2
,
entity
.
getType
());
ps
.
executeUpdate
();
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Chyba při komunikaci s databází: "
+
e
.
getMessage
());
throw
new
NoConnectionException
();
}
}
@Override
public
void
update
(
TypeEntity
entity
)
throws
NoConnectionException
,
DataEntryException
{
try
(
Connection
con
=
getConnection
();)
{
con
.
setAutoCommit
(
false
);
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
UPDATE
))
{
ps
.
setString
(
1
,
entity
.
getType
());
ps
.
setInt
(
2
,
entity
.
getId
());
ps
.
executeUpdate
();
con
.
commit
();
con
.
setAutoCommit
(
true
);
}
catch
(
SQLException
e
)
{
con
.
rollback
();
con
.
setAutoCommit
(
true
);
System
.
err
.
println
(
"Chyba při práci s databází: "
+
e
.
getMessage
());
throw
new
DataEntryException
();
}
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Chyba při komunikaci s databází: "
+
e
.
getMessage
());
throw
new
NoConnectionException
();
}
}
@Override
public
void
delete
(
TypeEntity
entity
)
throws
NoConnectionException
{
try
(
Connection
con
=
getConnection
();
PreparedStatement
ps
=
con
.
prepareStatement
(
DELETE
))
{
ps
.
setInt
(
1
,
entity
.
getId
());
ps
.
executeUpdate
();
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Chyba při komunikaci s databází: "
+
e
.
getMessage
());
throw
new
NoConnectionException
();
}
}
}
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