How does a PreparedStatement avoid or prevent SQL injection?

前端 未结 10 1838
再見小時候
再見小時候 2020-11-22 05:21

I know that PreparedStatements avoid/prevent SQL Injection. How does it do that? Will the final form query that is constructed using PreparedStatements will be a string or o

10条回答
  •  無奈伤痛
    2020-11-22 05:27

    I guess it will be a string. But the input parameters will be sent to the database & appropriate cast/conversions will be applied prior to creating an actual SQL statement.

    To give you an example, it might try and see if the CAST/Conversion works.
    If it works, it could create a final statement out of it.

       SELECT * From MyTable WHERE param = CAST('10; DROP TABLE Other' AS varchar(30))
    

    Try an example with a SQL statement accepting a numeric parameter.
    Now, try passing a string variable (with numeric content that is acceptable as numeric parameter). Does it raise any error?

    Now, try passing a string variable (with content that is not acceptable as numeric parameter). See what happens?

提交回复
热议问题