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());
}
}