java链接数据库以及对数据库的操作

package cn.edu.xbmu.dao;

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

import cn.edu.xbmu.models.UserModel;

public class UserDao {
private DBHelper helper=new DBHelper();
/**
*
* 添加用户
* @param model
* @return
* @throws Exception
*/
public int save(UserModel model)throws Exception{
int i=0;
try {
String sql=”insert into user(userName,userPass) values(?,?)”;
Object values[]=new Object[]{model.getUserName(),model.getUserPass()};
i=helper.executeUpdate(sql, values);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
helper.close();
}
return i;
}
/**
*
* 修改用户信息
* @param model
* @return
* @throws Exception
*/
public int update(UserModel model)throws Exception{
int result=0;
try {
String sql=”update user set userName=?,userPass=? where id=?”;
result=helper.executeUpdate(sql,model.getUserName(),model.getUserPass(),model.getId());

    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }finally{
        helper.close();
    }
    return result;
}
/**
 * 
 * 删除用户信息
 * @param id
 * @return
 * @throws Exception
 */
public int delete(int id)throws Exception{
    int result=0;
    try {
        String sql="delete from `user` where id=?";
        result=helper.executeUpdate(sql, id);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }finally{
        helper.close();
    }
    return result;
}

/**
 * 
 * 用户登录
 */
public  UserModel login(UserModel model)throws Exception{
    UserModel result=null;
    try {
        String sql="select * from `user` where userName=? and userPass=?";
        ResultSet rs=helper.executeQuery(sql,model.getUserName(),model.getUserPass());
        if(rs.next()){
            result =new UserModel();
            result.setUserName(rs.getString("userName"));
            result.setUserPass(rs.getString("userPass"));
            result.setId(rs.getInt("id"));
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    finally{
        helper.close();
    }
    return result;
}
/**
 * 
 * 在数据库中查找
 * @param userName
 * @return
 * @throws Exception
 */
public boolean findByName(String userName)throws Exception{
    try {
        String sql="select count(1) from `user` where userName=? ";
        int i=helper.executeScalar(sql, userName);
        if(i>0){
            return true;
        }else{
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }finally{
        helper.close();
    }
}
/**
 * 查找所有用户
 * @return
 * @throws Exception
 */
public List<UserModel> findAll()throws Exception{
    List<UserModel> list =new ArrayList<UserModel>();
    try {
        String sql="select * from `user`";
        ResultSet rs=helper.executeQuery(sql);
        while(rs.next()){
        UserModel m=new UserModel();
        m.setId(rs.getInt("id"));
        m.setUserName(rs.getString("userName"));
        m.setUserPass(rs.getString("userPass"));
        list.add(m);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }finally{
        helper.close();
    }
    return list;
}
/**
 * 
 * 查找某个人
 * @param id
 * @return
 * @throws Exception
 */
public UserModel findById(int id)throws Exception{
    ResultSet rs=null;
    UserModel model=null;
    try {
        String sql="select * from `user` where id=?";
        rs=helper.executeQuery(sql, id);
        if(rs.next()){
            int id1=rs.getInt("id");
            String userName=rs.getString("userName");
            String userPass=rs.getString("userPass");
            model=new UserModel(id1,userName,userPass);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }finally{
        helper.close();
    }
    return model;
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值