jdbc的知识

  1. 现在既不需要
     Driver driver = new com.mysql.jdbc.Driver();
     DriverManager.registerDriver(driver);
    
    也不需要
    Class.forName("com.mysql.jdbc.Driver");
    
    使用下面的自动注册,wq是数据库的名称,前面是固定的
    String url = "jdbc:mysql://localhost:3306/wq";
    Connection connection = DriverManager.getConnection(url, "root", "1234");
    
  2. 预防sql注入,使用prepareStatement;
    String url = "select * from wq where p_name = ? and p_type = ?";
    PreparedStatement preparedStatement = connection.prepareStatement(url);
    preparedStatement.setString(1, "vivo");
    preparedStatement.setInt(2, 2);
    
    sql注入:
    String url = "select * from wq where name = 'a' or 'a' = 'a' and password = 'a' or 'a' = 'a' ";
    

你可能感兴趣的:(jdbc的知识)