增刪查改的實現

package DAO;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;java

import tools.DBHelper;
import entity.User;sql

public class userDAO {
 
 public List getAllUser(){//查詢get

  String SQL="select * from user";
  ResultSet rs=DBHelper.getResultSet(SQL);
  return tools.tools.resultSetToList(rs, User.class);
 }it

 
 public static boolean add(User u)//添加數據
 {
  String sql="INSERT INTO user(userID,userName,password) "+"VALUES(?,?,?)";
  Object[] params=new Object[]{u.getUserID(),u.getUserName(),u.getPassword()}; 
  int n=DBHelper.ExecSql(sql, params);
  if(n>0)
   return true;
  else
   return false;
  
 }
 public static boolean delete(User u)//刪除數據
 {
  String sql="DELETE FROM user WHERE userID=?";
  Object[] params=new Object[]{u.getUserID()};
  int n=DBHelper.ExecSql(sql, params);
  if(n>0)
   return true;
  else
   return false;
 }
 public static boolean update(User u)//修改數據
 {
  String sql="update user set userName=?,password=? where userID=?";
  Object[] params=new Object[]{u.getUserName(), u.getPassword(), u.getUserID()};
  int n=DBHelper.ExecSql(sql, params);
  if(n>0)
   return true;
  else
   return false;
 }
 
}class

相關文章
相關標籤/搜索