• 10月9日 Jdbc(2)


    PreparedStatement的使用和jdbcUtil工具类的封装

    拼接带来的sql注入问题(拼接sql)

    Statement

    PreparedStatement的使用

    1. package com.fs.db;
    2. import com.fs.util.JdbcUtil;
    3. import java.sql.*;
    4. import java.util.Date;
    5. import java.util.Scanner;
    6. /**
    7. * 模拟SQL攻击
    8. */
    9. public class Demo4 {
    10. public static void main(String[] args) {
    11. boolean flag = login2("zs", "zs");
    12. //SQL攻击 SQL注入
    13. //boolean flag = login2("asdasd' or '1' = '1", "zs' or '1'='1");
    14. if(flag){
    15. System.out.println("登录成功");
    16. }else{
    17. System.out.println("登录失败");
    18. }
    19. }
    20. /**
    21. * 登录方法
    22. * @param username 用户名
    23. * @param password 密码
    24. * @return true登录成功, false: 登录失败
    25. */
    26. public static boolean login(String username,String password){
    27. Connection conn = null;
    28. Statement statement = null;
    29. ResultSet resultSet = null;
    30. try {
    31. Class.forName("com.mysql.jdbc.Driver");
    32. String url = "jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8&useSSL=false";
    33. conn = DriverManager.getConnection(url, "root", "123");
    34. statement = conn.createStatement();
    35. String sql = "select * from user where username = '"+username+"' and password='"+password+"'";
    36. System.out.println(sql);
    37. resultSet = statement.executeQuery(sql);
    38. return resultSet.next();
    39. } catch (ClassNotFoundException e) {
    40. e.printStackTrace();
    41. } catch (SQLException throwables) {
    42. throwables.printStackTrace();
    43. }finally{
    44. //倒序关
    45. try {
    46. if(resultSet != null) {
    47. resultSet.close();
    48. }
    49. if(statement != null) {
    50. statement.close();
    51. }
    52. if(conn != null) {
    53. conn.close();
    54. }
    55. } catch (SQLException throwables) {
    56. throwables.printStackTrace();
    57. }
    58. }
    59. return false;
    60. }
    61. /**
    62. * 使用PreparedStatement
    63. * 登录方法
    64. * @param username 用户名
    65. * @param password 密码
    66. * @return true登录成功, false: 登录失败
    67. */
    68. public static boolean login2(String username,String password){
    69. Connection conn = null;
    70. PreparedStatement pstmt = null;
    71. ResultSet resultSet = null;
    72. try {
    73. Class.forName("com.mysql.jdbc.Driver");
    74. String url = "jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8&useSSL=false";
    75. conn = DriverManager.getConnection(url, "root", "123");
    76. //SQL语句使用?占位
    77. String sql = "select * from user where username = ? and password=? ";
    78. pstmt = conn.prepareStatement(sql);
    79. //给?赋值setXxx(?序号,值) 从1开始 通用类型: setObject()
    80. pstmt.setString(1,username);
    81. pstmt.setString(2,password);
    82. //执行sql 调用无参的方法
    83. resultSet = pstmt.executeQuery();
    84. return resultSet.next();
    85. } catch (ClassNotFoundException e) {
    86. e.printStackTrace();
    87. } catch (SQLException throwables) {
    88. throwables.printStackTrace();
    89. }finally{
    90. //倒序关
    91. try {
    92. if(resultSet != null) {
    93. resultSet.close();
    94. }
    95. if(pstmt != null) {
    96. pstmt.close();
    97. }
    98. if(conn != null) {
    99. conn.close();
    100. }
    101. } catch (SQLException throwables) {
    102. throwables.printStackTrace();
    103. }
    104. }
    105. return false;
    106. }
    107. /**
    108. * 使用PreparedStatement
    109. * 登录方法
    110. * @param username 用户名
    111. * @param password 密码
    112. * @return true登录成功, false: 登录失败
    113. */
    114. public static boolean login3(String username,String password){
    115. Connection conn = null;
    116. PreparedStatement pstmt = null;
    117. ResultSet resultSet = null;
    118. try {
    119. conn = JdbcUtil.getConnection();
    120. //SQL语句使用?占位
    121. String sql = "select * from user where username = ? and password=? ";
    122. pstmt = conn.prepareStatement(sql);
    123. //给?赋值setXxx(?序号,值) 从1开始 通用类型: setObject()
    124. pstmt.setString(1,username);
    125. pstmt.setString(2,password);
    126. //执行sql 调用无参的方法
    127. resultSet = pstmt.executeQuery();
    128. return resultSet.next();
    129. } catch (SQLException throwables) {
    130. throwables.printStackTrace();
    131. }finally{
    132. JdbcUtil.close(conn,pstmt,resultSet);
    133. }
    134. return false;
    135. }
    136. }

    和com包同级

    jdbc.driverclass=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8&useSSL=false
    jdbc.username=root
    jdbc.password=123

    Jdbc的工具类 

    jdbc的优化  代码重复问题 参数优化问题

    第一步:把数据库四大参数放到properties文件
    jdbc.driverclass=com.mysql.jdbc.Driver
    jdbc.url=jdbc :mysql : / / localhost:3306/test2?
    useUnicode=true&characterEncoding=utf8&useSSL=falsejdbc.username=root
    jdbc.password=123
    第二步:编写一个jdbc的工具类,封装重复代码 (工具类不要让别人可以new出来,所以搞成静态的,返回一个connection对象,直接用类型.属性调用方法和属性)

    1. package com.fs.util;
    2. import java.io.IOException;
    3. import java.io.InputStream;
    4. import java.sql.*;
    5. import java.util.Properties;
    6. /**
    7. * JDBC的工具类
    8. */
    9. public class JdbcUtil {
    10. private static Properties props = new Properties();
    11. static{
    12. //加载db.properties文件
    13. try {
    14. //写的绝对路径, 一旦项目拷贝到另外一台电脑,路径可能错误的
    15. //FileInputStream fis = new FileInputStream("C:\\java6\\jdbc\\code\\demo1\\src\\db.properties");
    16. //使用相对路径, 相对于src目录
    17. //getClassLoader()得到该类的类加载器
    18. InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
    19. props.load(in);
    20. Class.forName(props.getProperty("jdbc.driverclass"));
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. } catch (ClassNotFoundException e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. //得到连接的方法
    28. public static Connection getConnection() throws SQLException {
    29. return DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.username"), props.getProperty("jdbc.password"));
    30. }
    31. //关闭资源的方法
    32. public static void close(Connection conn, PreparedStatement pstmt, ResultSet resultSet){
    33. try {
    34. if(resultSet != null) {
    35. resultSet.close();
    36. }
    37. if(pstmt != null) {
    38. pstmt.close();
    39. }
    40. if(conn != null) {
    41. conn.close();
    42. }
    43. } catch (SQLException throwables) {
    44. throwables.printStackTrace();
    45. }
    46. }
    47. }

    从此用PreparedStatement替代Statement

  • 相关阅读:
    Linux 部分很实用的命令使用详解
    数学推理题:张王李赵陈五对夫妇聚会,见面握手
    策略枚举:消除在项目里大批量使用if-else的正确姿势
    SSM框架学习——Spring之容器
    SpringCloud Gateway 服务网关的快速入门
    第五章 树 24 AcWing 1636. 最低公共祖先
    分布式唯一Id,它比GUID好
    Fast Planner 轨迹规划
    道可云元宇宙每日资讯|上海旅游节将打造数字文旅新场景
    1 opencv-python图像读写模块
  • 原文地址:https://blog.csdn.net/weixin_53415999/article/details/133961428