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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Iago Gómez Salgado
daaexample
Commits
2a02263d
Commit
2a02263d
authored
Feb 26, 2017
by
Iago Gómez Salgado
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add PetsDao and Pet Entity
parent
6a7ab6bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
247 additions
and
0 deletions
+247
-0
PetsDAO.java
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
+165
-0
Pet.java
src/main/java/es/uvigo/esei/daa/entities/Pet.java
+82
-0
No files found.
src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java
0 → 100644
View file @
2a02263d
package
es
.
uvigo
.
esei
.
daa
.
dao
;
import
java.util.logging.Logger
;
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
es.uvigo.esei.daa.entities.Pet
;
public
class
PetsDAO
extends
DAO
{
private
final
static
Logger
LOG
=
Logger
.
getLogger
(
PetsDAO
.
class
.
getName
());
public
Pet
get
(
int
id
)
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"SELECT * FROM pets WHERE id=?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
try
(
final
ResultSet
result
=
statement
.
executeQuery
())
{
if
(
result
.
next
())
{
return
rowToEntity
(
result
);
}
else
{
throw
new
IllegalArgumentException
(
"Invalid id"
);
}
}
}
}
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 pets"
;
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
>
peoplePets
(
int
peopleId
)
throws
DAOException
{
try
(
final
Connection
conn
=
this
.
getConnection
()){
final
String
query
=
"SELECT * FROM pets WHERE owner= ?"
;
try
(
final
PreparedStatement
stmt
=
conn
.
prepareStatement
(
query
))
{
stmt
.
setInt
(
1
,
peopleId
);
try
(
final
ResultSet
result
=
stmt
.
executeQuery
())
{
final
List
<
Pet
>
pets
=
new
LinkedList
<>();
while
(
result
.
next
())
{
pets
.
add
(
rowToEntity
(
result
));
}
return
pets
;
}
}
}
catch
(
SQLException
e
){
LOG
.
log
(
Level
.
SEVERE
,
"Error peoplePets"
,
e
);
throw
new
DAOException
(
e
);
}
}
public
Pet
add
(
Pet
pet
)
throws
DAOException
,
IllegalArgumentException
{
if
(
pet
==
null
)
{
throw
new
IllegalArgumentException
(
"Pet is null!"
);
}
try
(
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"INSERT INTO pets(name, kind, breed, owner) VALUES (?,?,?,?)"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
,
Statement
.
RETURN_GENERATED_KEYS
))
{
statement
.
setString
(
1
,
pet
.
getName
());
statement
.
setString
(
2
,
pet
.
getKind
());
statement
.
setString
(
3
,
pet
.
getBreed
());
statement
.
setInt
(
4
,
pet
.
getOwner
());
if
(
statement
.
executeUpdate
()
==
1
)
{
return
pet
;
}
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 pets SET name=?, kind=?, breed=?, owner=? WHERE id=?"
;
try
(
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setString
(
1
,
pet
.
getName
());
statement
.
setString
(
2
,
pet
.
getKind
());
statement
.
setString
(
3
,
pet
.
getBreed
());
statement
.
setInt
(
4
,
pet
.
getOwner
());
statement
.
setInt
(
5
,
pet
.
getId
());
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"name, kind and breed can't be null"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error modifying a pet"
,
e
);
throw
new
DAOException
();
}
}
public
void
delete
(
int
id
)
throws
DAOException
,
IllegalArgumentException
{
try
(
final
Connection
conn
=
this
.
getConnection
())
{
final
String
query
=
"DELETE FROM pets WHERE id= ?"
;
try
(
final
PreparedStatement
statement
=
conn
.
prepareStatement
(
query
))
{
statement
.
setInt
(
1
,
id
);
if
(
statement
.
executeUpdate
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"Invalid id"
);
}
}
}
catch
(
SQLException
e
)
{
LOG
.
log
(
Level
.
SEVERE
,
"Error deleting pet: "
+
id
,
e
);
throw
new
DAOException
(
e
);
}
}
private
Pet
rowToEntity
(
ResultSet
row
)
throws
SQLException
{
return
new
Pet
(
row
.
getInt
(
"id"
),
row
.
getString
(
"name"
),
row
.
getString
(
"kind"
),
row
.
getString
(
"breed"
),
row
.
getInt
(
"owner"
)
);
}
}
src/main/java/es/uvigo/esei/daa/entities/Pet.java
0 → 100644
View file @
2a02263d
package
es
.
uvigo
.
esei
.
daa
.
entities
;
public
class
Pet
{
private
int
id
;
private
String
name
;
private
String
kind
;
private
String
breed
;
private
int
owner
;
Pet
(){}
public
Pet
(
int
id
,
String
name
,
String
kind
,
String
breed
,
int
owner
)
{
this
.
setName
(
name
);
this
.
setKind
(
kind
);
this
.
setBreed
(
breed
);
this
.
setOwner
(
owner
);
}
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getKind
()
{
return
kind
;
}
public
void
setKind
(
String
kind
)
{
this
.
kind
=
kind
;
}
public
String
getBreed
()
{
return
breed
;
}
public
void
setBreed
(
String
breed
)
{
this
.
breed
=
breed
;
}
public
int
getOwner
()
{
return
owner
;
}
public
void
setOwner
(
int
owner
)
{
this
.
owner
=
owner
;
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
(
int
)
(
prime
*
result
+
id
);
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(!(
obj
instanceof
Pet
))
return
false
;
Pet
other
=
(
Pet
)
obj
;
if
(
id
!=
other
.
id
)
return
false
;
return
true
;
}
}
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