JDBC的DAO封装

步骤:
1、创建包名dao
2、使用工具类来封装CRUD的操作:实体类名+DAO+Impl
3、注意:如果实体类的属性少,可以直接把属性作为参数传递
4、使用接口来约束方法和参数:实体类名+DAO
只有接口才能约束方法和参数,只能按照接口的规范来实现方法
工具类就实现接口中的方法

数据库表创建

create table users(
id int primary key auto_increment,
username varchar(50),
password varchar(50)

);

接口

package com.m.DAO;

import java.util.List;

import com.m.domain.Users;

public interface usersDAO {
	//增加
	public  Integer insertFun(Users user) throws Exception;
	//删除
	public Integer delFunById(int i)throws Exception;
	//修改 密码
	public Integer upFunById(int i,String s)throws Exception;
	//查询所有
	public List<Users> selFun()throws Exception;
	//通过id查询
	public boolean selFunById(int i)throws Exception;
	

}

实现类

package com.m.DAO;



import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.m.JDBCUtils.JDBCUtils;
import com.m.domain.Users;

public class usersDAOImpl implements usersDAO {
		

		@Override
		public Integer insertFun(Users user) throws IOException, Exception  {
			Connection conn = JDBCUtils.getConnection();
			String sql="insert into users (username,password) values(?,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			int i = ps.executeUpdate();
			return i;
			
		}

		@Override
		public Integer delFunById(int i) throws IOException, Exception {
			Connection conn = JDBCUtils.getConnection();
			String sql="delete  from users where id=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			
			ps.setInt(1, i);
			
			int j = ps.executeUpdate();
			
			return j;
		}

		@Override
		public Integer upFunById(int i,String s) throws IOException, Exception {
			
			Connection conn = JDBCUtils.getConnection();
			String sql="update users set password=? where id=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, s);
			ps.setInt(2, i);
			int j = ps.executeUpdate();
			return j;
		}

		@Override
		public List<Users> selFun() throws IOException, Exception {
			List<Users> list=new ArrayList<>();
			Connection conn = JDBCUtils.getConnection();
			String sql="select * from users";
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				list.add(new Users(rs.getInt(1),rs.getString(2),rs.getString(3)));
			}
			return list;
		}

		@Override
		public boolean selFunById(int i) throws IOException, Exception {
			List<Users> list=new ArrayList<>();
			Connection conn = JDBCUtils.getConnection();
			String sql="select * from users where id=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1,i);
			ResultSet rs = ps.executeQuery();
			
			return rs.next();
		}
}


测试类

package com.m.demo2;

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.m.DAO.usersDAO;
import com.m.DAO.usersDAOImpl;
import com.m.domain.Users;

class DAOtest {

	@Test
	void test() throws IOException, Exception {
		usersDAO u=new usersDAOImpl();
		Integer i = u.delFunById(4);
		if(i>0) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
		List<Users> list = u.selFun();
		list.stream().forEach(a->System.out.println(a));
		
		i= u.upFunById(3, "741");
		if(i>0) {
			System.out.println("修改密码成功");
		}else {
			System.out.println("修改密码失败");
		}
	}

}

结果
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值