首页 > 其他分享 >PreparedStatement对象

PreparedStatement对象

时间:2022-10-16 23:44:26浏览次数:42  
标签:PreparedStatement java 对象 st JdbcUtils sql import conn

PreparedStatement可以防止SQL注入。效率更高!

1、新增

package com.hua.lesson03;

import com.hua.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class TestInsert {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = JdbcUtils.getConnection();

            //区别
            //使用?占位符 代替参数
            String sql = "INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) values(?,?,?,?,?)";
            st = conn.prepareStatement(sql);//预编译SQL,先写SQL,然后不执行

            //手动给参数赋值
            st.setInt(1,6);
            st.setString(2,"lisi");
            st.setString(3,"126466");
            st.setString(4,"[email protected]");

            //注意点: sql.Date    数据库   sql.Date
            //        util.Date   Java   new Date().getTime()  获得时间戳
            st.setDate(5,new java.sql.Date(new Date().getTime()));

            //执行
            int i = st.executeUpdate();
            if(i>0){
                System.out.println("插入成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,null);
        }

    }
}

2、删除

package com.hua.lesson03;

import com.hua.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

public class Testdelete {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = JdbcUtils.getConnection();

            //区别
            //使用?占位符 代替参数
            String sql = "delete from users where id = ?";
            st = conn.prepareStatement(sql);//预编译SQL,先写SQL,然后不执行

            //手动给参数赋值
            st.setInt(1,6);

            //执行
            int i = st.executeUpdate();
            if(i>0){
                System.out.println("删除成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,null);
        }
    }
}

3、修改

package com.hua.lesson03;

import com.hua.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

public class TestUpdate {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = JdbcUtils.getConnection();

            //区别
            //使用?占位符 代替参数
            String sql = "update users set `NAME` = ?  where id = ? ;";
            st = conn.prepareStatement(sql);//预编译SQL,先写SQL,然后不执行

            //手动给参数赋值
            st.setString(1,"杨不悔");
            st.setInt(2,1);

            //执行
            int i = st.executeUpdate();
            if(i>0){
                System.out.println("修改成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,null);
        }
    }
}

4、查询

package com.hua.lesson03;

import com.hua.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestSelect {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtils.getConnection();

            String sql = "select * from users where id = ?";//编写SQL

            st = conn.prepareStatement(sql);//预编译

            st.setInt(1,1);//传递参数

            rs = st.executeQuery();//执行

            while (rs.next()){
                System.out.println(rs.getString("NAME"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,rs);
        }
    }
}

5、防止SQL注入

package com.hua.lesson03;

import com.hua.lesson02.utils.JdbcUtils;

import java.sql.*;

public class SQLInjection {
    public static void main(String[] args) {
//        login("lisi","123456");
      login("' ' or '1=1","' ' or '1=1");
    }

    //登录业务
    public static void login(String username,String password){
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            //PreparedStatement防止SQL注入的本质,把传递进来的参数当做字符
            //假设其中存在转义字符,比如说 ' 会被直接转义
            String sql = "select * from users where `NAME` = ? and `password` = ?";//mybatis

             st = conn.prepareStatement(sql);

             st.setString(1,username);
             st.setString(2,password);
            //SQl
            //select * from users where `NAME` = '' or '1=1' and `password` = ''  or '1=1'

            rs = st.executeQuery();//查询完毕会返回一个结果集

            while (rs.next()){
                System.out.println(rs.getString("NAME"));
                System.out.println(rs.getString("password"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,rs);
        }

    }
}

 

标签:PreparedStatement,java,对象,st,JdbcUtils,sql,import,conn
From: https://www.cnblogs.com/1982king/p/16797636.html

相关文章

  • 实验二 类与对象(2)
    任务4:Complex.hpp1#pragmaonce2#include<bits/stdc++.h>3usingnamespacestd;4classComplex{5public:6Complex(doubler=0,doubl......
  • 内置函数,可迭代对象
    一、重要内置函数1.zip()将对不同列表中对应的元素打包成一个个元组,然后返回由这些元组组成的对象.  用list()转换后打印出结果,可以看到输出结果为一个列表,列表中的......
  • python新类似乎违背了广度优先的执行顺序, 对象自定义计数实例化的多少
    classTSSS():deff1(self):print('fromTSSS')classSSS(TSSS):deff1(self):print('fromSSS')classSS():deff1(self):......
  • Vue事件对象如何调用
    <script>exportdefault{ data(){  return{   message:0,   put1:'www.96net.com.cn',  } }, methods:{  dianji(e){   ......
  • Servlet对象的生命周期
    Servlet对象的生命周期什么是Servlet对象生命周期?Servlet对象什么时候被创建。Servlet对象什么时候被销毁。Servlet对象创建了几个?Servlet对象的生命周期表示:一个Ser......
  • 可迭代对象和迭代器对象以及for循环的本质
    目录一、可迭代对象二、迭代器对象迭代器介绍:迭代器对象:迭代器对象的作用迭代器对象实操:注意事项三、for循环的本质一、可迭代对象之前我们对于for循环为什么可以遍历没......
  • 实验2 类和对象(2)
    实验任务4#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;//定义Complex类classComplex{public:Complex(doubler=0,doublei=......
  • 实验二 类和对象(2)
    4.实验任务4不使用C++标准库,自行设计并实现一个简化版复数类Complex。Complex.hpp: #pragmaonce#include<iostream>#include<string>#include<iomanip>#incl......
  • 实验2 类与对象(2)
    一、实验内容:实验任务4:1.Complex.hpp:#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublex=0,doubley=0......
  • 实验2 类和对象
    实验4#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doubler=0.0,doublei=0.0):real(r),i......