public
class
PersonDAOImpl
implements
PersonDAO
{
public
void insert(Person person) throws Exception
{
String sql =
"INSERT INTO person (id,name,password,age,email) VALUES (?,?,?,?,?)"
;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc =
new
DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,person.getId()) ;
pstmt.setString(2,person.getName()) ;
pstmt.setString(3,person.getPassword()) ;
pstmt.setInt(4,person.getAge()) ;
pstmt.setString(5,person.getEmail()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch
(Exception e)
{
throw
new
Exception(
"操作出现异常"
) ;
}
finally
{
dbc.close() ;
}
}
public
void update(Person person) throws Exception
{
String sql =
"UPDATE person SET name=?,password=?,age=?,email=? WHERE id=?"
;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc =
new
DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,person.getName()) ;
pstmt.setString(2,person.getPassword()) ;
pstmt.setInt(3,person.getAge()) ;
pstmt.setString(4,person.getEmail()) ;
pstmt.setString(5,person.getId()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch
(Exception e)
{
throw
new
Exception(
"操作出现异常"
) ;
}
finally
{
dbc.close() ;
}
}
public
void
delete
(String id) throws Exception
{
String sql =
"DELETE FROM person WHERE id=?"
;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc =
new
DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,id) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch
(Exception e)
{
throw
new
Exception(
"操作出现异常"
) ;
}
finally
{
dbc.close() ;
}
}
public
Person queryById(String id) throws Exception
{
Person person = null ;
String sql =
"SELECT id,name,password,age,email FROM person WHERE id=?"
;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc =
new
DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,id) ;
ResultSet rs = pstmt.executeQuery() ;
if
(rs.next())
{
person =
new
Person() ;
person.setId(rs.getString(1)) ;
person.setName(rs.getString(2)) ;
person.setPassword(rs.getString(3)) ;
person.setAge(rs.getInt(4)) ;
person.setEmail(rs.getString(5)) ;
}
rs.close() ;
pstmt.close() ;
}
catch
(Exception e)
{
throw
new
Exception(
"操作出现异常"
) ;
}
finally
{
dbc.close() ;
}
return
person ;
}
public
List queryAll() throws Exception
{
List all =
new
ArrayList() ;
String sql =
"SELECT id,name,password,age,email FROM person"
;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc =
new
DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery() ;
while
(rs.next())
{
Person person =
new
Person() ;
person.setId(rs.getString(1)) ;
person.setName(rs.getString(2)) ;
person.setPassword(rs.getString(3)) ;
person.setAge(rs.getInt(4)) ;
person.setEmail(rs.getString(5)) ;
all.add(person) ;
}
rs.close() ;
pstmt.close() ;
}
catch
(Exception e)
{
throw
new
Exception(
"操作出现异常"
) ;
}
finally
{
dbc.close() ;
}
return
all ;
}
public
List queryByLike(String cond) throws Exception
{
List all =
new
ArrayList() ;
String sql =
"SELECT id,name,password,age,email FROM person WHERE name LIKE ? or email LIKE ?"
;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
try
{
dbc =
new
DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,
"%"
+cond+
"%"
) ;
pstmt.setString(2,
"%"
+cond+
"%"
) ;
ResultSet rs = pstmt.executeQuery() ;
while
(rs.next())
{
Person person =
new
Person() ;
person.setId(rs.getString(1)) ;
person.setName(rs.getString(2)) ;
person.setPassword(rs.getString(3)) ;
person.setAge(rs.getInt(4)) ;
person.setEmail(rs.getString(5)) ;
all.add(person) ;
}
rs.close() ;
pstmt.close() ;
}
catch
(Exception e)
{
throw
new
Exception(
"操作出现异常"
) ;
}
finally
{
dbc.close() ;
}
return
all ;
}
};