Skip to content

Commit 374681b

Browse files
Create SQLInjectionExample.java
1 parent f1e6ed9 commit 374681b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

SQLInjectionExample.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Security Vulnerability #3757
2+
// The below code is for the security vulnerability that has been programmed by me in java itself. You can simply convert my code to your preferred programming language and then edit it and after that use it.
3+
4+
import java.sql.Connection;
5+
import java.sql.DriverManager;
6+
import java.sql.ResultSet;
7+
import java.sql.Statement;
8+
import java.util.Scanner;
9+
10+
public class SQLInjectionExample {
11+
public static void main(String[] args) {
12+
Scanner scanner = new Scanner(System.in);
13+
System.out.print("Enter username: ");
14+
String username = scanner.nextLine();
15+
16+
System.out.print("Enter password: ");
17+
String password = scanner.nextLine();
18+
19+
try {
20+
// Connect to the database
21+
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
22+
Statement statement = connection.createStatement();
23+
24+
// Vulnerable SQL query (user inputs are not sanitized)
25+
String query = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "'";
26+
27+
ResultSet resultSet = statement.executeQuery(query);
28+
29+
// Checking if login is successful
30+
if (resultSet.next()) {
31+
System.out.println("Login successful!");
32+
} else {
33+
System.out.println("Login failed!");
34+
}
35+
36+
connection.close();
37+
} catch (Exception e) {
38+
e.printStackTrace();
39+
}
40+
scanner.close();
41+
}
42+
}

0 commit comments

Comments
 (0)