package five.daoImpl;
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 five.dao.UserDao;
import five.dao.myException.MyUserException;
import five.domain.User;
import five.utils.Utils;
//新建一个接口的实现类 观察 New Java Class interface superClass
public class UserDaoImpl implements UserDao {
public void addUser(User user) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = Utils.getConnection();
String sqlString = " insert into user(id, name, brithday, monny, password) value(?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sqlString);
preparedStatement.setInt(1, user.getId());
preparedStatement.setString(2, user.getName());
preparedStatement.setFloat(4, user.getMonney());
preparedStatement.setString(5, user.getPassword());
preparedStatement.setDate(3, new java.sql.Date(user.getBrithday()
.getTime()));
// 4.执行语句
int i = preparedStatement.executeUpdate();
System.out.println("新建" + i + "条记录");
} catch (SQLException e) {
// 为什么 捕获了异常 不能什么也不做?
// 原因 1 不利于debug
// 原因 2 异常 转移
// 例如 在注册用户 出现异常 没有任何处理
// 那么 注册出现异常时, 这个异常就被隐藏掉了
// 当用户 在检索 更新 这个用户的时候 就可能
// 出现异常了。由 系统就 转移。
// 怎样处理 异常?
// 方法一 捕获解决掉(打印)
// 但是这个异常应该 抛出到业务层, 作处理,提醒用户再注册
// 怎样抛出异常?
// 原封不动的抛出去?那样 就要修改接口的签名, (实现接口类抛出的异常是
// 接口声明异常的子集。)
// 导致的结果是 业务层在调用接口实现业务逻辑时, 就要处理 持久层的逻辑
// 有悖于 MVC 三层构架 的思想 (将jdbc 转换成 hibernate?)
// 解决 捕获到异常(编译异常)转换成的 运行异常 抛出去
// 编译异常 必须要处理 运行异常 不是必须要处理
throw new MyUserException(e.getMessage(), e);
} finally {
Utils.free(connection, preparedStatement, null);
}
}
public void deleteUser(User user) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = Utils.getConnection();
st = conn.createStatement();
String sql = "delete from user where id =" + user.getId();
int i = st.executeUpdate(sql);
System.out.println("删除" + i + "条记录");
} catch (SQLException e) {
throw new MyUserException(e.getMessage(), e);
} finally {
Utils.free(conn,st, rs);
}
}
private User mapingUser(ResultSet rs) throws SQLException {
User user = new User(
rs.getInt("id"),
rs.getString("name"),
rs.getString("password"),
new java.sql.Date(rs.getDate("brithday").getTime()),
rs.getFloat("monny")
);
return user;
}
public List<User> findUser(String name, String password) {
// 以下 使用 摸板模式 策略模式 优化以下 冗余代码
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// 以上 使用 摸板模式 策略模式 优化以上 冗余代码
try {
// 2.建立连接
conn = Utils.getConnection();
// 3.创建语句
// 4.执行语句
String sql = "select " +
"id, name, monny, brithday,password " +
"from user " +
"where " +
"name=" + name +
"password = " + password;
ps = conn.prepareStatement(sql);
ps.executeQuery(sql);
// 5.处理结果
List<User> resList = new ArrayList<User> ();
while (rs.next()) {
User user = mapingUser(rs);
resList.add(user);
}
//不可 直接返回 结果集
return resList;
// 以下 使用 摸板模式 策略模式 优化以下 冗余代码
} catch (SQLException e) {
throw new MyUserException(e.getMessage(), e);
} finally {
// 即使 不释放 结果集 释放掉conn后 结果集也是拿不到的
// 桥拆了 什么也没有了
Utils.free(conn, ps, rs);
}
// 以上 使用 摸板模式 策略模式 优化以上下 冗余代码
}
public User getUser(int userId) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
// 2.建立连接
conn = Utils.getConnection();
String sql = "select " +
"id, name, monny, brithday, password " +
"from " +
"user " +
"where id=? ";
ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
// 4.执行语句
rs = ps.executeQuery();
// 5.处理结果
while (rs.next()) {
if(rs.isLast()){
user = mapingUser(rs);
} else{
throw new MyUserException("wrong fk ");
}
}
}catch(SQLException e){
throw new MyUserException(e.getMessage(), e);
}
finally {
Utils.free(conn,ps, rs);
}
return user;
}
public void updataUser(User user) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = Utils.getConnection();
String sql = "update user set name=?, brithday=?, monny=? where id=? ";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setDate(2, new java.sql.Date(user.getBrithday().getTime()));
ps.setFloat(3, user.getMonney());
ps.setInt(4, user.getId());
ps.executeUpdate();
} catch (SQLException e) {
throw new MyUserException(e.getMessage(), e);
} finally {
Utils.free(conn, ps, rs);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package five.test;
import java.sql.Date;
import five.dao.UserDao;
import five.daoImpl.UserDaoImpl;
import five.domain.User;
public class UserDaoTest {
public static void main(String[] args) {
User user = new User(99, null, "asd", null, 0);
user.setBrithday(new Date(0));
user.setName("dao name1");
user.setMonney(1000.0f);
//依赖 具体实现 对策 工厂模式 通过配置文件
UserDao ud = new UserDaoImpl();
//UserDao ud = DaoFactory.getInstance().getUserDao();
ud.addUser(user);
user = ud.getUser(0);
System.out.print(user.getPassword());
}
}