java简单实现增删改查的方法

package dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import cn.jdbc.JDBCUtils;
import entity.User;
/**
 * DAO类
 * 负责访问数据库
 * @author soft01
 *
 */
public class UserDao{
/**
* 用于将所有用户查询出来,每一个用户的信息对应一个user对象,返回一个由这些user对象组成集和对象
* @return
* @throws Exception 
*/
public List<User> findAll() throws Exception{
List<User> listuser = new ArrayList<User>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from t_user";
try {
conn = JDBCUtils.getconn();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
User user = new User();
//给user对象敷值
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
listuser.add(user);//把user对象放到集和当中
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally {
JDBCUtils.close(rs, ps, conn);
}
return listuser;
}
/**
* 添加用户
* @param user
* @return
* @throws Exception 
*/
public int save(User user) throws Exception {
int rows = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "insert into t_user values(null,?,?,?)";
try {
conn = JDBCUtils.getconn();
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getEmail());
rows = ps.executeUpdate();
} catch (Exception e) {
throw e;
}finally {
JDBCUtils.close(rs, ps, conn);
}
return rows;
}
/**
* 删除用户
* @return
* @throws Exception 
*/
public int del(int id) throws Exception {
int rows = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "delete from t_user where id=?";
try {
conn = JDBCUtils.getconn();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rows = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw e;//将异常抛出,让调用着去解决
}finally {
JDBCUtils.close(rs, ps, conn);
}
return rows;
}
/**
* 修改用户名
* @return
*/
public int updateuname(User user) throws Exception {
int rows = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "update t_user set username=? where email=?";
//update t_user set username='admin1' where email='67890@qq.com';
try {
conn = JDBCUtils.getconn();
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getEmail());
rows = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally {
JDBCUtils.close(rs, ps, conn);
}
return rows;
}
/**
* 修改用户密码
* @return
* @throws Exception 
*/
public int updatepwd(User user) throws Exception {
int rows = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs= null;
String sql= "update t_user set password=? where id=?";
try {
conn  = JDBCUtils.getconn();
ps = conn.prepareStatement(sql);
ps.setString(1, user.getPassword());
ps.setInt(2, user.getId());
rows = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally {
JDBCUtils.close(rs, ps, conn);
}
return rows;
}
/**
* 查询用户名的方法
* @param name
* @return
* @throws Exception
*/
//这种方法性能会比较好一点
  public User findl(String name) throws Exception{//根据用户名查询指定的信息,如果找不到,返回null
  User user = null;
  Connection conn = null;
PreparedStatement ps = null;
ResultSet rs= null;
String sql= "select * from t_user where username=?";
try{
conn  = JDBCUtils.getconn();
ps = conn.prepareStatement(sql);
ps.setString(1,name);
rs = ps.executeQuery();
if(rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(name);
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
}
}catch(Exception e) {
e.printStackTrace();
throw e;
}finally {
JDBCUtils.close(rs, ps, conn);
}
return user;

 
  
  


/**
* 用户登陆方法
* @param username
* @param password
* @return
* @throws Exception
*/

public boolean login(String username,String password) throws Exception {
boolean flag = false;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs= null;
String sql = "select username,password from t_user where username=? and password=?";
try {
conn  = JDBCUtils.getconn();
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
while(rs.next()) {
if(rs != null) {
flag=true;
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally {
JDBCUtils.close(rs, ps, conn);
}
return flag;
}
public List<User> limit(int page) throws Exception {//page是当前页数
ArrayList<User> userlist =new ArrayList<User>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs= null;
String sql = "select * from t_user limit ?,?";//页数-1*条数,条数
try {
conn = JDBCUtils.getconn();
ps = conn.prepareStatement(sql);
ps.setInt(1, (page-1)*5);
ps.setInt(1, 5);
rs = ps.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
User user = new User();
//给user对象敷值
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
userlist.add(user);//把user对象放到集和当中
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return userlist;
}

}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值