写在前面:
PreparedStatement 对象所代表的 SQL 语句中的参数用问号(?)来表示(?在SQL中表示占位符),调用 PreparedStatement 对象的 setXxx() 方法来设置这些参数. setXxx() 方法有两个参数,第一个参数是要设置的 SQL 语句中的参数的索引(从 1 开始),第二个是设置的 SQL 语句中的参数的值。
原文链接:https://blog.csdn.net/mofeigege/article/details/109459633
对于Statement执行SQL语句,首先需要进行书写SQL语句,然后在Statement.executeUpdate(sql)中执行SQL语句,很容易出现SQL注入问题。因为PreparedStatement是把传进来的参数当做字符,假设其中存在转义字符,比如容易出现SQL注入问题的''(引号),引号就会被直接转义。所以引入PreparedStatement,此方法可以先进行SQL的预编译,然后给SQL中的占位符进行赋值,然后直接进行执行executeUpdate()。
插入数据
package com.haoran.lesson3; import com.haoran.lesson2.utils.jdbcUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Date; public class preparedStatement { public static void main(String[] args) { Connection conn = null; PreparedStatement pst = null; try { conn= jdbcUtils.getConnection(); String sql="INSERT INTO `users`(id,`NAME`,`PASSWORD`,`email`,`birthday`)" + "VALUE (?,?,?,?,?)"; //预编译SQL,先写SQL,不执行,用占位符? pst=conn.prepareStatement(sql); //手动给参数赋值 pst.setInt(1,5); pst.setString(2,"xiaolonglong"); pst.setString(3,"123321"); pst.setString(4,"123123123@qq.com"); pst.setDate(5,new java.sql.Date(new Date().getTime())); //该处sql.Date()是数据库中的时间,但是需要参数,所以调用数据库中的Date().getTime()获得时间戳 int i = pst.executeUpdate();//executeUpdate直接执行,不用加参数sql; if (i>0){ System.out.println("插入成功"); } } catch (SQLException e) { e.printStackTrace(); }finally { jdbcUtils.release(conn,pst,null); } } }
删除数据
package com.haoran.lesson3; import com.haoran.lesson2.utils.jdbcUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class preparedStatement1 { public static void main(String[] args) { Connection conn = null; PreparedStatement pst = null; try { conn= jdbcUtils.getConnection(); String sql="DELETE FROM `users` WHERE id=?"; //预编译SQL,先写SQL,不执行,用占位符? pst=conn.prepareStatement(sql); //手动给参数赋值 pst.setInt(1,5); int i = pst.executeUpdate();//executeUpdate直接执行,不用加参数sql; if (i>0){ System.out.println("删除成功"); } } catch (SQLException e) { e.printStackTrace(); }finally { jdbcUtils.release(conn,pst,null); } } }
更新数据
package com.haoran.lesson3; import com.haoran.lesson2.utils.jdbcUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class preparedStatement2 { public static void main(String[] args) { Connection conn = null; PreparedStatement pst = null; try { conn= jdbcUtils.getConnection(); String sql="UPDATE `users` SET `NAME`=? WHERE id=?"; //预编译SQL,先写SQL,不执行,用占位符? pst=conn.prepareStatement(sql); //手动给参数赋值 pst.setString(1,"makabaka"); pst.setInt(2,3); int i = pst.executeUpdate();//executeUpdate直接执行,不用加参数sql; if (i>0){ System.out.println("更新成功"); } } catch (SQLException e) { e.printStackTrace(); }finally { jdbcUtils.release(conn,pst,null); } } }
查询数据
package com.haoran.lesson3; import com.haoran.lesson2.utils.jdbcUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class preparedStatementSelect { public static void main(String[] args) { Connection conn = null; PreparedStatement pst = null; ResultSet rs =null; try { conn= jdbcUtils.getConnection(); String sql="SELECT * FROM `users` WHERE id=?"; //预编译SQL,先写SQL,不执行,用占位符? pst=conn.prepareStatement(sql); //手动给参数赋值 pst.setInt(1,2); rs=pst.executeQuery(); if (rs.next()){ System.out.println(rs.getString("NAME")); } } catch (SQLException e) { e.printStackTrace(); }finally { jdbcUtils.release(conn,pst,rs); } } }