首页>代码>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;
}
}
最近下载更多
最近浏览更多
短巷拥猫 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日

