首页>代码>java使用jdbc实现各种类型添加删除修改数据>/JDBCInsertDeleteUpdate/JDBCInsertDeleteUpdate/src/com/bdqn/dao/DeleteDao.java
package com.bdqn.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bdqn.entity.Student;
import com.bdqn.util.DbUtil;

public class DeleteDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	// 使用int类型传递int类型参数
	public int StudentDelete(int id) {
		String sql = "delete from student where id=?";
		Object[] obj = { id };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			if (num > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}

	// 使用int类型传递对象
	public int StudentDelete(Student stu) {
		String sql = "delete from student where id=?";
		Object[] obj = { stu.getId() };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			if (num > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	// 使用int类型传递种类型多种类型参数
	public int StudentDelete(int id, String name) {
		String sql = "delete from student where id=? and name=?";
		Object[] obj = { id, name };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			if (num > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}

	// 使用int类型传递对象的多个属性
	public int StudentDelete1(Student stu) {
		String sql = "delete from student where id=? and name=?";
		Object[] obj = { stu.getId() };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			if (num > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	// 使用boolean类型传递多个参数
	public boolean StudentDelete2(int id, String name) {
		String sql = "delete from student where id=? and name=?";
		Object[] obj = { id, name };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			System.out.println(num);
			if (num > 0) {
				System.out.println("删除成功!");
				return true;
			} else {
				System.out.println("删除失败!");
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	// 使用boolean类型传对象的多个属性
	public boolean StudentDelete2(Student stu) {
		String sql = "delete from student where id=? and name=?";
		Object[] obj = { stu.getId(), stu.getName() };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			if (num > 0) {
				System.out.println("删除成功!");
				return true;
			} else {
				System.out.println("删除失败!");
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 后面的都很相似,我只用传递对象的方式
	 */
	// 使用String方式
	public String StudentDelete3(Student stus) {
		String sql = "delete from student where id=? and name=?";
		Object[] obj = { stus.getId(), stus.getName() };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			if (num > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 使用list方式
	public List<Student> StudentDelete4(Student stu) {
		String sql = "delete from student where id=? and name=?";
		Object[] obj = { stu.getId(), stu.getName() };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			if (num > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	//使用map方式
	public Map<String, String> StudentDelete5(Student stu){
		String sql = "delete from student where id=? and name=?";
		Object[] obj = { stu.getId(), stu.getName() };
		ps = DbUtil.executeSql(sql, obj);
		try {
			int num = ps.executeUpdate();
			if (num > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
最近下载更多
放鞭炮  LV1 2023年12月26日
mmmmmmppp  LV10 2022年8月21日
暖光女神  LV11 2022年8月10日
Myangyyyy  LV10 2022年4月28日
凉冰丶  LV7 2021年12月22日
lironggang  LV38 2021年9月11日
最代码1996  LV11 2021年8月13日
歪歪歪  LV8 2021年4月26日
苏语凌  LV1 2021年4月9日
ewan007  LV29 2021年1月28日
最近浏览更多
放鞭炮  LV1 2023年12月26日
微信网友_5986558820093952  LV3 2023年12月26日
asddwh  LV12 2023年12月25日
dai呆代  LV1 2023年12月3日
321170193  LV6 2023年11月12日
szy20001006  LV2 2023年6月16日
哇呜呜  LV1 2023年6月13日
遗留问题  LV2 2023年6月9日
bismarcktirppitz  LV2 2023年6月4日
刘一天  LV2 2023年4月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友