/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-22 11:40:18
* Note: 子类DAO
*/
public class UserDAO extends BaseDAO implements IUserDAO {
public void insert(User user) {
String name = user.getName();
int age = user.getAge().intValue();
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn.createStatement();
stmt.execute("INSERT INTO user (name,age) " + "VALUES('" + name + "'," + age + ")");
} catch (SQLException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public User find(Integer id) {
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(
"SELECT * FROM user WHERE id=" + id.intValue());
if (result.next()) {
Integer i = new Integer(result.getInt(1));
String name = result.getString(2);
Integer age = new Integer(result.getInt(3));
User user = new User();
user.setId(i);
user.setName(name);
user.setAge(age);
return user;
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
}
--------------------next---------------------
阅读(347) | 评论(0) | 转发(0) |