Understanding SQL Injection: Types and Prevention

HOME

String username = request.getParameter("username");
String forename = request.getParameter("forename");

String sql = "SELECT * FROM users WHERE username = '" + username + "' AND forename = '" + forename + "'";

Connection conn = DriverManager.getConnection(url, username, forename);
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(sql);
if (result.next()) {
    // User is authenticated
    String status = result.getString("success");
    System.out.println("Login to the application");
} else {
    // Authentication failed
  System.out.println("Unable to Login");
}

SELECT * FROM users WHERE username = 'admin'  AND forename = 'admin';

SELECT * FROM users WHERE username = 'admin'  -- AND forename = 'admin';

SELECT * FROM users WHERE username = 'admin'

String username = request.getParameter("username");
String forename = request.getParameter("forename");

String sql = "SELECT * FROM users WHERE username = ? AND forename = ?";

Connection conn = DriverManager.getConnection(url, username, forename);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, forename);
ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
    // User is authenticated
       String status = result.getString("success");
       System.out.println("Login to the application");
} else {
    // Authentication failed
    System.out.println("Unable to Login");
}

SELECT ProductName, ProductDescription, ProductCost
FROM Products
WHERE ProductId = '100' UNION SELECT Username, Password FROM Users;

SQL Multiple Choice Answers – MCQ3

HOME

SQL Multiple Choice Questions – MCQ3

























SQL Multiple Choice Answers – MCQ2

HOME

SQL Multiple Choice Questions – MCQ2




















REVOKE INSERT, UPDATE ON Database.* FROM user123;





SQL Multiple Choice Answers – MCQ1

HOME

SQL Multiple Choice Questions – MCQ1


CREATE TABLE Students (ID int PRIMARY KEY, Name varchar(50));

SELECT column1, column2, ... FROM table_name;





UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

DELETE FROM table_name WHERE condition;



ALTER TABLE table_name
ADD column_name datatype;



a) SELECT FirstName FROM Students

a) SELECT * FROM Students

a) SELECT * FROM Students WHERE FirstName='Peter'

a) SELECT * FROM Students WHERE FirstName LIKE 'a%'


CREATE TABLE Students (ID int PRIMARY KEY, Name varchar(50));




SQL Multiple Choice Questions – MCQ1

HOME

Answer


Answer


Answer


Answer


Answer


Answer


DROP TABLE Student;

Answer


Answer


Answer


Answer


Answer


Answer


Answer


Answer


CREATE TABEL Students (ID int, Name varchar(50));

Answer


Answer


a) SELECT * FROM Students
b) SELECT [all] FROM Students
c) SELECT *.Students
d) SELECT Students

Answer


a) SELECT * FROM Students WHERE FirstName='Peter'
b) SELECT * FROM Students WHERE FirstName<>'Peter'
c) SELECT [all] FROM Students WHERE FirstName LIKE 'Peter'
d) SELECT [all] FROM Students WHERE FirstName='Peter'

Answer


a) SELECT * FROM Students WHERE FirstName LIKE 'a%'
b) SELECT * FROM Students WHERE FirstName='%a%'
c) SELECT * FROM Students WHERE FirstName='a'
d) SELECT * FROM Students WHERE FirstName LIKE '%a'

Answer


Answer


CREATE TABLE Students (ID int PRIMARY, Name varchar(50));

Answer


ALTER TABLE Students ADD COLUMN Email varchar(50);

Answer


Answer


SELECT Name FROM Employees WHERE Age > 30 AND Department = 'HR';

Answer

====================================================================

SQL Multiple Choice Questions – MCQ2
SQL Multiple Choice Questions – MCQ3

How to insert data in SQL Server using Java

HOME

jdbc:<driver protocol>:<driver connection details>
MS MySql Server - jdbc:odbc:DemoDSN
MySQL - jdbc:mysql://localhost:3306/demodb
Oracle - jdbc:orac;e:thin@myserver:1521:demodb
String dbUrl = "jdbc:mysql://localhost:3306/demo";
String username = "student";
String password = "student1$";

Connection conn = DriverManager.getConnection(dbUrl,username,password)
Statement stmt = conn.createStatement();

 int rowAffected = stmt.executeUpdate(
                    "insert into employees (last_name, first_name, email, department,salary) values ('Singh', 'Vibha','vibha.test@gmail.com', 'QA', 85000)");

<dependencies>
    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <version>8.2.0</version>
</dependency>

package org.example;

import java.sql.*;

public class InsertRow_Demo {
    public static void main(String[] args) throws SQLException {

        Connection conn;
        Statement stmt = null;
        ResultSet result = null;
        ResultSet result1 = null;
        ResultSet result2 = null;

        String dbUrl = "jdbc:mysql://localhost:3306/demo";
        String username = "student";
        String password = "student1$";

        try {
            //Get a connection to database
            conn = DriverManager.getConnection(dbUrl, username, password);

            System.out.println("Database connection is successful\n");

            //Create a statement
            stmt = conn.createStatement();

            System.out.println("Inserting a new employee\n");

            int rowAffected = stmt.executeUpdate(
                    "insert into employees (last_name, first_name, email, department,salary) values ('Singh', 'Vibha','vibha.test@gmail.com', 'QA', 85000)");

            System.out.println("No of rows inserted :" + rowAffected);

            //Execute the SQL Query
            result = stmt.executeQuery("Select * from employees");

            //Process the result set
            while (result.next()) {
                System.out.println("First_Name :" + result.getString("first_name") + " , " + ("Last_Name :" + result.getString("last_name")));

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Multiple Choice Questions

HOME

  1. Programming Languages
    1. Java
  2. Test Automation Frameworks
    1. Selenium
    2. Advance Selenium
    3. Robot Framework
    4. JUnit4
    5. TestNG
  3. API Testing
    1. Rest API
    2. Pytest Framework
  4. DevOps & Continuous Integration/Continuous Deployment (CI/CD)
    1. DevOps
    2. Jenkins
  5. Version Control Systems
    1. Git
    2. GitHub
  6. Containerization
    1. Docker
  7. Database
    1. SQL
  8. Types of Testing
    1. Security Testing
    2. Performance Testing
    3. ETL Testing

Jenkins Multiple Choice Questions – MCQ1
Jenkins Multiple Choice Questions – MCQ2

GitHub Multiple Choice Questions – MCQ1

Docker – Basic Level – Multiple Choice Questions and Answers – MCQ1
Docker – Advance Level – Multiple Choice Questions and Answers – MCQ1