zoukankan      html  css  js  c++  java
  • statement 的延伸 ----》PreparedStatement

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;


    public class demo1 {

     /**
      * @param args
      */
     public static void main(String[] args) {
      // TODO Auto-generated method stub
      Connection conn = null;
      PreparedStatement pstmt = null;
      
       String sql = "UPDATE dog SET name=?,heath=?,love=? WHERE id=?";
      try {


       //加载数据库驱动
       
       Class.forName("com.mysql.jdbc.Driver");
       
       conn = DriverManager.getConnection("jdbc:mysql:///epet","root","root");
       
       pstmt = conn.prepareStatement(sql);
       
       pstmt.setString(1, "xiaoguo"); //括号中1指的是  sql中的name     2指的是 修改的参数
       pstmt.setInt(2, 12);
       pstmt.setInt(3, 15);
       pstmt.setInt(4, 2);
       
       pstmt.executeUpdate();
       System.out.println("成功");
       
      } catch (Exception e) {
       // TODO: handle exception
       e.printStackTrace();
      }finally{
       try {
        
        if (null != pstmt) {
         pstmt.close();
        }
        if (null != conn) {
         conn.close();
        }
       } catch (Exception e2) {
        // TODO: handle exception
       }
       
      }
      
      
     }

    }

  • 相关阅读:
    缓存
    vue 生命周期:
    mongodb 数据库 增删改查
    微信小程序左右分类滚动列表
    4月29日记
    什么是MVVM
    什么是mvc
    React路由
    TodoList案例
    React中兄弟组件传值
  • 原文地址:https://www.cnblogs.com/xibeifeng/p/6177937.html
Copyright © 2011-2022 走看看