package five.domain;
import java.sql.Date;
public class User {
private int id;
private String name;
private String password;
private Date brithday;
private float monney;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public float getMonney() {
return monney;
}
public void setMonney(float monney) {
this.monney = monney;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(int id, String name, String password, Date brithday,
float monney) {
super();
this.id = id;
this.name = name;
this.password = password;
this.brithday = brithday;
this.monney = monney;
}
public User() {
super();
}
public User(String name) {
this.name = name;
}
public String toString() {
return "name = " + this.name +
" id = " + this.id +
" brithday = " + this.brithday +
" monney = " + this.monney;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
package five.dao;
import java.util.List;
import five.domain.User;
public interface UserDao {
public void addUser(User user);
public User getUser(int userId);
public void updataUser(User user);
public void deleteUser(User user);
public List<User> findUser(String name, String password);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
package five.daoImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import five.dao.UserDao;
import five.dao.myException.MyUserException;
import five.domain.User;
import five.utils.Utils;
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, monney, 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) {
// TODO Auto-generated method stub
}
public List<User> findUser(String name, String password) {
// TODO Auto-generated method stub
return null;
}
public User getUser(int userId) {
// TODO Auto-generated method stub
return null;
}
public void updataUser(User user) {
// TODO Auto-generated method stub
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////