Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
DAAExample
Project
Project
Details
Activity
Releases
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
Óscar Servia González
DAAExample
Commits
5a372e59
Commit
5a372e59
authored
Mar 15, 2018
by
Óscar Servia González
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add petDAO
parent
cf4e4de2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
197 additions
and
5 deletions
+197
-5
mysql-with-inserts.sql
db/mysql-with-inserts.sql
+5
-5
PetDAO.java
src/main/java/es/uvigo/esei/daa/dao/PetDAO.java
+190
-0
UnitTestSuite.java
src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java
+2
-0
No files found.
db/mysql-with-inserts.sql
View file @
5a372e59
...
...
@@ -35,11 +35,11 @@ INSERT INTO `daaexample`.`people` (`id`,`name`,`surname`) VALUES (0,'Alba','Fern
INSERT
INTO
`daaexample`
.
`people`
(
`id`
,
`name`
,
`surname`
)
VALUES
(
0
,
'Asunción'
,
'Jiménez'
);
-- Add inserts to pets
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Jimminy'
,
0
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Cisco'
,
0
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Phoenix'
,
1
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Rocky'
,
1
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Tallulah'
,
2
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Jimminy'
,
1
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Cisco'
,
2
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Phoenix'
,
3
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Rocky'
,
3
);
INSERT
INTO
`daaexample`
.
`pet`
(
`idPet`
,
`name`
,
`idMaster`
)
VALUES
(
0
,
'Tallulah'
,
4
);
...
...
src/main/java/es/uvigo/esei/daa/dao/PetDAO.java
0 → 100644
View file @
5a372e59
package
es
.
uvigo
.
esei
.
daa
.
dao
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
es.uvigo.esei.daa.entities.Pet
;
public
class
PetDAO
extends
DAO
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PeopleDAO
.
class
.
getName
());
public
Pet
get
(
int
idPet
)
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pet WHERE idPet=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
idPet
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
if
(
result
.
next
())
{
return
rowToEntity
(
result
);
}
else
{
throw
new
IllegalArgumentException
(
"Invalid idPet"
);
}
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error getting a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
public
List
<
Pet
>
list
()
throws
DAOException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pet"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
final
List
<
Pet
>
pets
=
new
LinkedList
<>();
while
(
result
.
next
())
{
pets
.
add
(
rowToEntity
(
result
));
}
return
pets
;
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pets"
,
e
);
throw
new
DAOException
(
e
);
}
}
public
List
<
Pet
>
listAllPetsOfPerson
(
int
idMaster
)
throws
DAOException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pet WHERE idMaster=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
idMaster
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
final
List
<
Pet
>
pets
=
new
LinkedList
<>();
while
(
result
.
next
())
{
pets
.
add
(
rowToEntity
(
result
));
}
return
pets
;
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error listing pets"
,
e
);
throw
new
DAOException
(
e
);
}
}
public
Pet
add
(
String
name
,
int
idMaster
)
throws
DAOException
,
IllegalArgumentException
{
if
(
name
==
null
)
{
throw
new
IllegalArgumentException
(
"name can't be null"
);
}
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"INSERT INTO pet VALUES(null, ?, ?)"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
,
Statement
.
RETURN_GENERATED_KEYS
))
{
statement
.
setString
(
1
,
name
);
statement
.
setInt
(
2
,
idMaster
);
if
(
statement
.
executeUpdate
()
==
1
)
{
try
(
ResultSet
resultKeys
=
statement
.
getGeneratedKeys
())
{
if
(
resultKeys
.
next
())
{
return
new
Pet
(
resultKeys
.
getInt
(
1
),
name
,
idMaster
);
}
else
{
LOG
.
log
(
Level
.
SEVERE
,
"Error retrieving inserted id"
);
throw
new
SQLException
(
"Error retrieving inserted id"
);
}
}
}
else
{
LOG
.
log
(
Level
.
SEVERE
,
"Error inserting value"
);
throw
new
SQLException
(
"Error inserting value"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error adding a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
public
void
modify
(
Pet
pet
)
throws
DAOException
,
IllegalArgumentException
{
if
(
pet
==
null
)
{
throw
new
IllegalArgumentException
(
"pet can't be null"
);
}
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"UPDATE pet SET name=?, idMaster=? WHERE idPet=?"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setString
(
1
,
pet
.
getName
());
statement
.
setInt
(
2
,
pet
.
getIdMaster
());
statement
.
setInt
(
3
,
pet
.
getIdPet
());
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"name can't be null"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a pet"
,
e
);
throw
new
DAOException
();
}
}
public
void
delete
(
int
idPet
)
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"DELETE FROM pet WHERE idPet=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
idPet
);
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"Invalid idPet"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting a pet"
,
e
);
throw
new
DAOException
(
e
);
}
}
private
Pet
rowToEntity
(
ResultSet
row
)
throws
SQLException
{
return
new
Pet
(
row
.
getInt
(
"idPet"
),
row
.
getString
(
"name"
),
row
.
getInt
(
"idMaster"
)
);
}
}
\ No newline at end of file
src/test/java/es/uvigo/esei/daa/suites/UnitTestSuite.java
View file @
5a372e59
...
...
@@ -6,10 +6,12 @@ import org.junit.runners.Suite.SuiteClasses;
import
es.uvigo.esei.daa.dao.PeopleDAOUnitTest
;
import
es.uvigo.esei.daa.entities.PersonUnitTest
;
import
es.uvigo.esei.daa.entities.PetUnitTest
;
import
es.uvigo.esei.daa.rest.PeopleResourceUnitTest
;
@SuiteClasses
({
PersonUnitTest
.
class
,
PetUnitTest
.
class
,
PeopleDAOUnitTest
.
class
,
PeopleResourceUnitTest
.
class
})
...
...
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