PreparedStatement对象来实现JDBC增删改查

一. PreparedStatement与Statement的区别

1.不需要sql语句拼接,防止sql注入,更加安全

2.用占位符的方式写sql,便于后期维护,提高代码可读性,可以自动对类型进行转换

3.有预编译功能,可以大批量处理sql,(mysql不明显,Oracle很明显)

4.向数据库中添加一条数据

5.PreparedStatement:用于执行sql语句的对象

6.用connection的PreparedStatement(sql)方法获取

7.用executeUpdate(sql)执行sql语句

* 注意:只能执行 insert,update,delect,不能执行select

二.这是连接数据库的方法,放到此工具类中

package com.zhiyou.jdbc.connection;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCTools {
  
 /**
     * 这是连接数据库的方法,放到此工具类中
     * @return
     * @throws IOException 
     *
     *
     */

    static Connection con;
    static String url;
    static Properties properties;
    static{
 
       //1.创建properties对象
        properties = new Properties();
        try {
      
     //2.从类路径下加载db.properties文件
            properties.load(JDBCTools.class.getClassLoader().getResourceAsStream("db.properties"));
        
    //3.获取db.properties中加载的url信息
            url=properties.getProperty("url");
        } catch (IOException e) {
            e.printStackTrace();
        }
          
    }

    //4.通过DriverManager的getConnection()方法获取数据库连接

    public static Connection getConnections(){
        try {
            con = DriverManager.getConnection(url,properties);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
     
    }

    /**
      *这是关闭数据库的工具方法,因为曾删改都需要用,所以写在此工具方法中
    */

    public static void close(Statement statement,Connection con){
        try {
            statement.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
    }

}

1、增加

public void testPreparedStatement(){
      Connection con=null;
      PreparedStatement pstmt=null;//创建PreparedStatement对象
      try {
     
     //1、准备Connection连接数据库
            con=JDBCTools.getConnections();
   
      //2、准备sql语句
          //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。

          String sql="insert into users(name,age,email) values(?,?,?)";
          pstmt =con.prepareStatement(sql);
     
    //4、占位符设置值
          pstmt.setString(1, "zdm");
          pstmt.setInt(2, 27);
          pstmt.setString(3, "[email protected]");
   
      //5、执行sql
          pstmt.executeUpdate();
     } catch (SQLException e) {
        e.printStackTrace();
     }finally{

         //6、关闭数据库等
         JDBCTools.close(pstmt, con); 
     }
    }

2、修改 

 public void testPreparedStatement(){
          Connection con=null;
          PreparedStatement pstmt=null;
//创建PreparedStatement对象
            try {
                con=JDBCTools.getConnection();
            
   //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。
                String sql="update users set name=?,age=?,email=? where id=?";
        
       //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。
               pstmt=con.prepareStatement(sql);
               pstmt.setString(1, "asas");
                pstmt.setInt(2, 12);
                pstmt.setString(3, "asa@jjj");
                pstmt.setInt(4, 11);
      
         //执行sql
                preStatement.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{

             //关闭数据库等
              JDBCTools.close(pstmt, con);
            }
        }

4、查询

public void PreparedStatement(){
        Connection con=null;
        PreparedStatement pstmt=null;
//创建PreparedStatement对象
        ResultSet res=null;
      try {
     
    //1.准备Connection连接数据库
          

          con=JDBCTools.getConnections();            
          //2.准备sql字符串
          
          String sql="select * from users where id=8";
     
    //3.准备prepareStatement
          

          pstmt=con.prepareStatement(sql);
          
 
        //4.执行sql得到ResultSet
          

          res = pstmt.executeQuery();
          
   
      //5.处理ResultSet显示查询到的结果
          

          while(res.next()){
              int id=res.getInt(1);
              String name=res.getString(2);
              int age=res.getInt(6);
              String email=res.getString(4);
              System.out.println("id"+id+"\t name"+name+"\t email"+email);
          }
          
      } catch (Exception e) {
          e.printStackTrace();
      }finally{
   
    //6.关闭
          JDBCTools.close(res, pstmt, con);
      }
    }

猜你喜欢

转载自blog.csdn.net/weixin_39944884/article/details/81121958