• 使用PreparedStatement接口实现增删改操作


    直接上下代码:

     1 package com.learn.jdbc.chap04.sec02;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 
     6 import com.learn.jdbc.model.Album;
     7 import com.learn.jdbc.util.DbUtil;
     8 /**
     9  * 使用PreparedStatement接口实现增删改操作
    10  * @author Administrator
    11  *
    12  */
    13 public class Demo1 {
    14 
    15     private static DbUtil dbUtil=new DbUtil();
    16     /**
    17      * 使用PreparedStatement 预编译 添加数据
    18      * @param ab
    19      * @return
    20      * @throws Exception
    21      */
    22     private static int addAlbum(Album ab) throws Exception{
    23         Connection con=dbUtil.getCon(); // 获取连接
    24         String sql="insert into sp_album values (null,?,?,?)";
    25         PreparedStatement pstmt = con.prepareStatement(sql);
    26         pstmt.setString(1,ab.getName());
    27         pstmt.setInt(2,ab.getUid());
    28         pstmt.setLong(3,ab.getTime());
    29         int result = pstmt.executeUpdate();
    30         dbUtil.close(pstmt, con);
    31         return result;
    32     }
    33     
    34     public static void main(String[] args) throws Exception {
    35         int result = addAlbum(new Album("亲王", 6, System.currentTimeMillis()));
    36         if(result>0){
    37             System.out.println("数据插入成功!");
    38         }else{
    39             System.out.println("数据插入失败!");
    40         }
    41     }
    42 }
     1 package com.learn.jdbc.chap04.sec02;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 
     6 import com.learn.jdbc.model.Album;
     7 import com.learn.jdbc.util.DbUtil;
     8 /**
     9  * 使用PreparedStatement接口实现增删改操作
    10  * @author Administrator
    11  *
    12  */
    13 public class Demo2 {
    14     private static DbUtil dbUtil=new DbUtil();
    15     /**
    16      * 使用PreparedStatement 预编译 修改数据
    17      * @param ab
    18      * @return
    19      * @throws Exception
    20      */
    21     private static int updateAlbum(Album ab) throws Exception{
    22         Connection con=dbUtil.getCon(); // 获取连接
    23         String sql="update sp_album set name=?,uid=?,add_time=? where id=?";
    24         PreparedStatement pstmt = con.prepareStatement(sql);
    25         pstmt.setString(1,ab.getName());
    26         pstmt.setInt(2,ab.getUid());
    27         pstmt.setLong(3,ab.getTime());
    28         pstmt.setInt(4,ab.getId());
    29         int result = pstmt.executeUpdate();
    30         dbUtil.close(pstmt, con);
    31         return result;
    32     }
    33     
    34     public static void main(String[] args) throws Exception {
    35         int result = updateAlbum(new Album(9,"亲王66", 8, System.currentTimeMillis()));
    36         if(result>0){
    37             System.out.println("数据修改成功!");
    38         }else{
    39             System.out.println("数据修改失败!");
    40         }
    41     }
    42 }
     1 package com.learn.jdbc.chap04.sec02;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 
     6 import com.learn.jdbc.util.DbUtil;
     7 /**
     8  * 使用PreparedStatement接口实现增删改操作
     9  * @author Administrator
    10  *
    11  */
    12 public class Demo3 {
    13     private static DbUtil dbUtil=new DbUtil();
    14     
    15     /**
    16      * 使用PreparedStatement 预编译 删除数据
    17      * @param ab
    18      * @return
    19      * @throws Exception
    20      */
    21     private static int deleteAlbum(int id) throws Exception{
    22         Connection con=dbUtil.getCon(); // 获取连接
    23         String sql="delete from sp_album where id=?";
    24         PreparedStatement pstmt = con.prepareStatement(sql);
    25         pstmt.setInt(1,id);
    26         int result = pstmt.executeUpdate();
    27         dbUtil.close(pstmt, con);
    28         return result;
    29     }
    30     
    31     public static void main(String[] args) throws Exception{
    32         int result = deleteAlbum(15);
    33         if(result>0){
    34             System.out.println("数据删除成功!");
    35         }else{
    36             System.out.println("数据删除失败!");
    37         }
    38     }
    39 }
  • 相关阅读:
    职场篇:聚焦与复盘
    职场篇:直面情绪杀手【已补更】
    .NetCore实践篇:成功解决分布式监控ZipKin聚合依赖问题(三)
    职场篇:为何我们需要思想大洗礼?
    职场篇:从温水煮青蛙说起
    .NetCore实践篇:分布式监控系统zipkin踩坑之路(二)
    postman application/json;
    yapi 个人空间 这个分组的问题
    yapi 的分组的理解!
    yapi的安装
  • 原文地址:https://www.cnblogs.com/eaglezb/p/6055312.html
Copyright © 2020-2023  润新知