statement和Prestatement的区别

public class StatementCRUDtest { 

  /** 
    * 操作表jdbc_users 
    * @param args 
    */ 
  public static void main(String[] args) { 
    User u=new User(); 
    u.setId(45); 
    u.setName("statement"); 
    u.setPasswd("yf123"); 
    u.setPhone("13821930"); 
    u.setEmail("yf@163.com"); 
    //insert(u); 

    //delete(2); 

    //reset(u); 

    System.out.println(getById(45)); 
  } 
  /**增*/ 
  public static void insert(User user){ 
    Connection conn=null; 
    Statement stmt=null; 
    try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      //conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123"); 
      stmt=conn.createStatement();//Statement创建时就是一个空的执行器 
      /**在execute或者executeQuery时执行死的sql语句*/ 
      /**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/ 
      stmt.execute("insert into jdbc_users values ("+user.getId()+",'"+user.getName()+"','"+user.getPasswd()+"','"+user.getPhone()+"','"+user.getEmail()+"')"); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    }finally{ 
      if(stmt!=null){try{stmt.close();}catch(Exception e){}} 
      if(conn!=null){try{conn.close();}catch(Exception e){}} 
    } 
  } 

  /**删*/ 
  public static void delete(Integer id){ 
    Connection conn=null; 
    Statement stmt=null; 
    try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      //conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123"); 
      stmt=conn.createStatement();//Statement创建时就是一个空的执行器 
      /**在execute或者executeQuery时执行死的sql语句*/ 
      /**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/ 
      stmt.execute("delete from jdbc_users where id="+id); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    }finally{ 
      if(stmt!=null){try{stmt.close();}catch(Exception e){}} 
      if(conn!=null){try{conn.close();}catch(Exception e){}} 
    } 
  } 

  /**改*/ 
  public static void reset(User user){ 
    Connection conn=null; 
    Statement stmt=null; 
    try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      //conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123"); 
      stmt=conn.createStatement();//Statement创建时就是一个空的执行器 
      /**在execute或者executeQuery时执行死的sql语句*/ 
      /**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/ 
      String sql="update jdbc_users set name='"+user.getName()+"',passwd='"+user.getPasswd()+"',phone='"+user.getPhone()+"',email='"+user.getEmail()+"' where id="+user.getId(); 
      System.out.println(sql); 
      stmt.execute(sql); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    }finally{ 
      if(stmt!=null){try{stmt.close();}catch(Exception e){}} 
      if(conn!=null){try{conn.close();}catch(Exception e){}} 
    } 
  } 

  /**查*/ 
  public static User getById(Integer id){ 
    Connection conn=null; 
    Statement stmt=null; 
    ResultSet rs=null; 
    User u=null; 
    try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      //conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123"); 
      stmt=conn.createStatement();//Statement创建时就是一个空的执行器 
      /**在execute或者executeQuery时执行死的sql语句*/ 
      /**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/ 
      String sql="select * from jdbc_users where id="+id; 
      System.out.println(sql); 
      rs=stmt.executeQuery(sql); 
      while(rs.next()){ 
        u=new User(); 
        u.setId(rs.getInt("id")); 
        u.setName(rs.getString("name")); 
        u.setPhone(rs.getString("phone")); 
        u.setPasswd(rs.getString("passwd")); 
        u.setEmail(rs.getString("email")); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
    }finally{ 
      if(stmt!=null){try{stmt.close();}catch(Exception e){}} 
      if(conn!=null){try{conn.close();}catch(Exception e){}} 
    } 
    return u; 
  } 
} 
/** 
* PreparedStatement是在创建pstm的时候就给它传一个动态的sql,参数是通过pstm设置的。执行时,只需要空执行一下就可以. 
* @author Administrator 
* 
*/ 
public class PreparedStatementCRUDtest { 

  /** 
    * 操作表jdbc_users 
    * @param args 
    */ 
  public static void main(String[] args) { 
    User u=new User(); 
    u.setId(21); 
    u.setName("statement"); 
    u.setPasswd("yf123"); 
    u.setPhone("13821930"); 
    u.setEmail("yf@163.com"); 
    //insert(u); 

    //delete(42); 

    //reset(u); 

    System.out.println(getById(21)); 
  } 
  /**增*/ 
  public static void insert(User user){ 
    Connection conn=null; 
    PreparedStatement pstmt=null; 
    try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      //conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123"); 
      String sql="insert into jdbc_users values(?,?,?,?,?)"; 
      pstmt=conn.prepareStatement(sql);//PreparedStatement创建时就传过去一个sql语句,这样就可以预编译 
      /**然后设置sql中好占位符的值,这里是动态的传参数*/ 
      pstmt.setInt(1, user.getId()); 
      pstmt.setString(2, user.getName()); 
      pstmt.setString(3, user.getPasswd()); 
      pstmt.setString(4, user.getPhone()); 
      pstmt.setString(5, user.getEmail()); 
      /**设置好后,就全封装到pstm里了,只要空执行就可以了*/ 
      pstmt.execute(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    }finally{ 
      if(pstmt!=null){try{pstmt.close();}catch(Exception e){}} 
      if(conn!=null){try{conn.close();}catch(Exception e){}} 
    } 
  } 

  /**删*/ 
  public static void delete(Integer id){ 
    Connection conn=null; 
    PreparedStatement pstmt=null; 
    try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      //conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123"); 
      String sql="delete from jdbc_users where id=?"; 
      /**PreparedStatement创建时就传过去一个sql语句,这样就可以预编译*/ 
      pstmt=conn.prepareStatement(sql); 
      /**然后设置sql中好占位符的值,这里是动态的传参数*/ 
      pstmt.setInt(1, id); 
      /**设置好后,就全封装到pstm里了,只要空执行就可以了*/ 
      pstmt.execute(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    }finally{ 
      if(pstmt!=null){try{pstmt.close();}catch(Exception e){}} 
      if(conn!=null){try{conn.close();}catch(Exception e){}} 
    } 
  } 

  /**改*/ 
  public static void reset(User u){ 
    Connection conn=null; 
    PreparedStatement pstmt=null; 
    try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      //conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123"); 
      String sql="update jdbc_users set name=?,passwd=?,phone=?,email=? where id=?"; 
      /**PreparedStatement创建时就传过去一个sql语句,这样就可以预编译*/ 
      pstmt=conn.prepareStatement(sql); 
      /**然后设置sql中好占位符的值,这里是动态的传参数*/ 
      pstmt.setString(1, u.getName()); 
      pstmt.setString(2, u.getPasswd()); 
      pstmt.setString(3, u.getPhone()); 
      pstmt.setString(4, u.getEmail()); 
      pstmt.setInt(5, u.getId()); 
      /**设置好后,就全封装到pstm里了,只要空执行就可以了*/ 
      pstmt.execute(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    }finally{ 
      if(pstmt!=null){try{pstmt.close();}catch(Exception e){}} 
      if(conn!=null){try{conn.close();}catch(Exception e){}} 
    } 
  } 

  /**查*/ 
  public static User getById(Integer id){ 
    Connection conn=null; 
    PreparedStatement pstmt=null; 
    ResultSet rs=null; 
    User u=null; 
    try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      //conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL10","scott","yf123"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL10", "SCOTT", "yf123"); 
      String sql="select * from jdbc_users where id=?"; 
      pstmt=conn.prepareStatement(sql);//Statement创建时就是一个空的执行器 
      /**在execute或者executeQuery时执行死的sql语句*/ 
      /**这只能是拼好的字符串,而不能动态的传参数,并且在数据库中每次肯定穿的是不同的sql语句,因此每次都要解析编译*/ 
      pstmt.setInt(1, id); 
      rs=pstmt.executeQuery(); 
      while(rs.next()){ 
        u=new User(); 
        u.setId(rs.getInt("id")); 
        u.setName(rs.getString("name")); 
        u.setPhone(rs.getString("phone")); 
        u.setPasswd(rs.getString("passwd")); 
        u.setEmail(rs.getString("email")); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
    }finally{ 
      if(pstmt!=null){try{pstmt.close();}catch(Exception e){}} 
      if(conn!=null){try{conn.close();}catch(Exception e){}} 
    } 
    return u; 
  } 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值