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

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

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

public class UpdateDao {
	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs = null;
	
	//使用int类型传递参数
	public int StudentUpdate(String name ,int classid,String name1,int classid1){
		String sql = "Update student set name=?,classid=? where name=? and classid=?";
		Object [] obj = {name,classid,name1,classid1};
		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 classid;
	}
	
	//使用int类型传递对象
	public int StudentUpdate(Student stu,Student stu1){
		String sql = "Update student set name=?,classid=? where name=? and classid=?";
		Object [] obj = {stu.getName(),stu.getClassid(),stu1.getName(),stu1.getClassid()};
		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 StudentUpdate1(Student stu,Student stu1){
		String sql = "Update student set name=?,classid=? where name=? and classid=?";
		Object [] obj = {stu.getName(),stu.getClassid(),stu1.getName(),stu1.getClassid()};
		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;
	}
	
	//使用list方式
	public List<Student> StudentUpdate2(Student stu,Student stu1){
		String sql = "Update student set name=?,classid=? where name=? and classid=?";
		Object [] obj = {stu.getName(),stu.getClassid(),stu1.getName(),stu1.getClassid()};
		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<Student, Student> StudentUpdate3(Student stu,Student stu1){
		String sql = "Update student set name=?,classid=? where name=? and classid=?";
		Object [] obj = {stu.getName(),stu.getClassid(),stu1.getName(),stu1.getClassid()};
		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日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友