首页>代码>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;
}
}
最近下载更多
最近浏览更多
短巷拥猫 LV12
5月23日
放鞭炮 LV1
2023年12月26日
微信网友_5986558820093952 LV4
2023年12月26日
asddwh LV13
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日

