1、PreparedStatement接口引入

  PreparedStatement是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement在操作时,

  是先在数据表中准备好了一条SQL语句,但是此SQL语句的具体内容暂时不设置,而是之后再进行设置。

  (以后开发一般使用PreparedStatement,不用Statement)

2、使用PreparedStatement接口实现添加数据操作

public class JDBCDemo5 {
private static MysqlUtil dbUtil = new MysqlUtil();
/**
* 添加emp
* @param emp
* @return
* @throws Exception
*/
private static int addEmp(Emp emp) throws Exception{
Connection conn = dbUtil.getConnection();
String sql = "insert into emp2 values(null,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, emp.getName());
pstmt.setDouble(2, emp.getSalary());
pstmt.setInt(3, emp.getAge());
int result = pstmt.executeUpdate();
dbUtil.close(pstmt, conn);
return result; } public static void main(String[] args) throws Exception {
Emp emp = new Emp("pengpeng",13000,27);
int result = addEmp(emp);
if(result==1){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
} } }

3、使用PreparedStatement接口实现更新数据操作

public class JDBCDemo6 {

    private static MysqlUtil dbUtil = new MysqlUtil();
private static int updateEmp(Emp emp) throws Exception{
Connection conn = dbUtil.getConnection();
String sql = "update emp2 set name=?,salary=?,age=? where id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, emp.getName());
pstmt.setDouble(2, emp.getSalary());
pstmt.setInt(3, emp.getAge());
pstmt.setInt(4, emp.getId());
int result = pstmt.executeUpdate();
dbUtil.close(pstmt, conn);
return result;
}
public static void main(String[] args) throws Exception {
Emp emp = new Emp(4,"pengpeng",13000,27);
int result = updateEmp(emp);
if(result==1){
System.out.println("update成功");
}else{
System.out.println("update失败");
} } }

4、使用PreparedStatement接口实现删除数据操作

public class JDBCDemo7 {
private static MysqlUtil dbUtil = new MysqlUtil();
private static int deleteEmp(int id) throws Exception{
Connection conn = dbUtil.getConnection();
String sql = "delete from emp2 where id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int result = pstmt.executeUpdate();
dbUtil.close(pstmt, conn);
return result;
} public static void main(String[] args) throws Exception{
Emp emp = new Emp(4,"pengpeng",13000,27);
int result = deleteEmp(4);
if(result==1){
System.out.println("delete成功");
}else{
System.out.println("delete失败");
} } }

最新文章

  1. VS2015中GLAUX库的链接问题
  2. 如何将一个对象存到网页中并在js中使用
  3. 创意欣赏:20幅字体排版(Typography)素描
  4. js表单提交一种方式
  5. HTML第一部分
  6. 三 JSP 技术
  7. 【数学】XMU 1593 找数字
  8. 使用Dataset
  9. Android UI SurfaceView的使用-绘制组合图型,并使其移动
  10. Unity3D-Shader-人物残影效果
  11. springboot添加swagger2组件
  12. Redis 协议为例谈简单的协议分析
  13. OI回忆录——一个过气OIer的制杖历程
  14. virtualbox+ievms:还你一个原装IE8
  15. CSS 中伪类的顺序
  16. centos7 使用二进制安装mysql 5.7.23
  17. ios中自定义图层的2种方法
  18. elastic job will never fire
  19. Memcached与KVDB的区别
  20. 使用netcat的正向 / 反向shell

热门文章

  1. python读取pop3服务器邮件并且下载
  2. Arduino SPI + SPI Flash芯片W25Q80BV
  3. TSQL--SORT MERGE JOIN
  4. 在 CentOS 上运行 ZKEACMS
  5. django DatabaseFunctions
  6. c++ inline使函数实现可以在头文件中,避免多重定义错误
  7. 1.IPtable基础命令总结
  8. drf序列化器serializers.SerializerMethodField()的用法
  9. [ActionScript 3.0] 获取TextFiled字符边框
  10. 「案例」让房东在 Airbnb 上展示他们的热情好客