标签:
1、新建Dao包 并创建实体的dao类
2、以DBHelper为基础具体实现增删查改
具体代码如下:
public static int insert(User u) {
String sql = "insert into user(userID,userName) values(?,?)";
Object[] params = new Object[] { u.getUserID(), u.getUserName() };
return DBHelper.ExecSql(sql, params);
}
public static int Del(User u) {
String sql = "delete from user where userID=? ";
Object[] params = new Object[] { u.getUserID() };
return DBHelper.ExecSql(sql, params);
}
public static int update(User u) {
String sql = "update user set userName=? where userID=?";
Object[] params = new Object[] { u.getUserName(), u.getUserID() };
return DBHelper.ExecSql(sql, params);
}
public static List<User> sel() throws ClassNotFoundException {
return ListToClass.RestoList(
DBHelper.getResultSet("select * from user "), User.class);
}
3、新建测试类,逐一测试。
查询测试代码如下:
List<User> users = useDao.sel();
for(User u:users)
{
System.out.print("ID:"+u.getUserID()+" ");
System.out.println("Name:"+u.getUserName());
}
标签:
原文地址:http://www.cnblogs.com/SunAutumn/p/5286745.html