步骤 2 : 基本的CRUD 步骤 3 : 非CRUD方法 步骤 4 : 相关界面功能截图-注册验证账号是否重复 package tmall.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import tmall.bean.User;
import tmall.util.DBUtil;
public class UserDAO {
public int getTotal() {
int total = 0;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from User";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public void add(User bean) {
String sql = "insert into user values(null ,? ,?)";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, bean.getName());
ps.setString(2, bean.getPassword());
ps.execute();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(User bean) {
String sql = "update user set name= ? , password = ? where id = ? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, bean.getName());
ps.setString(2, bean.getPassword());
ps.setInt(3, bean.getId());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "delete from User where id = " + id;
s.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public User get(int id) {
User bean = null;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select * from User where id = " + id;
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
bean = new User();
String name = rs.getString("name");
bean.setName(name);
String password = rs.getString("password");
bean.setPassword(password);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public List<User> list() {
return list(0, Short.MAX_VALUE);
}
public List<User> list(int start, int count) {
List<User> beans = new ArrayList<User>();
String sql = "select * from User order by id desc limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, start);
ps.setInt(2, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
User bean = new User();
int id = rs.getInt(1);
String name = rs.getString("name");
bean.setName(name);
String password = rs.getString("password");
bean.setPassword(password);
bean.setId(id);
beans.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return beans;
}
public boolean isExist(String name) {
User user = get(name);
return user!=null;
}
public User get(String name) {
User bean = null;
String sql = "select * from User where name = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, name);
ResultSet rs =ps.executeQuery();
if (rs.next()) {
bean = new User();
int id = rs.getInt("id");
bean.setName(name);
String password = rs.getString("password");
bean.setPassword(password);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public User get(String name, String password) {
User bean = null;
String sql = "select * from User where name = ? and password=?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, name);
ps.setString(2, password);
ResultSet rs =ps.executeQuery();
if (rs.next()) {
bean = new User();
int id = rs.getInt("id");
bean.setName(name);
bean.setPassword(password);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
}
基本的CRUD操作与CategoryDAO的CRUD一样,在此不做赘述
除开CRUD之外,UserDAO还提供了一些其他用于支持业务的方法。
在业务上,注册的时候,需要判断某个用户是否已经存在,账号密码是否正确等操作,UserDAO特别提供如下方法进行支持: 根据用户名获取对象 public User get(String name) 以boolean形式返回某个用户名是否已经存在 public boolean isExist(String name) 根据账号和密码获取对象,这才是合理的判断账号密码是否正确的方式,而不是一下把所有的用户信息查出来,在内存中进行比较。 public User get(String name,String password) 注:部分非CRUD的业务方法,需要结合业务场景才能更好的理解,现在理解不透彻也很正常,在后续学习到相关场景的时候,再回过头来看,就明白了。
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|