· 4 months ago · May 21, 2025, 08:40 PM
1import java.sql.*;
2
3public class LoginService {
4 private static final String DB_URL = "jdbc:mysql://localhost:3306/users?user=root&password=secret123";
5
6 public boolean authenticate(String username, String password) {
7 Connection conn = null;
8 Statement stmt = null;
9 ResultSet rs = null;
10
11 try {
12 conn = DriverManager.getConnection(DB_URL);
13 stmt = conn.createStatement();
14 String query = "SELECT * FROM users WHERE username = '" + username +
15 "' AND password = '" + password + "'";
16 rs = stmt.executeQuery(query);
17
18 if (rs.next()) {
19 return true;
20 }
21 } catch (SQLException e) {
22 e.printStackTrace();
23 } finally {
24 try {
25 if (rs != null) rs.close();
26 if (stmt != null) stmt.close();
27 if (conn != null) conn.close();
28 } catch (SQLException e) {
29 e.printStackTrace();
30 }
31 }
32 return false;
33 }
34
35 public static void main(String[] args) {
36 LoginService service = new LoginService();
37 boolean result = service.authenticate(args[0], args[1]);
38 System.out.println("Authentication " + (result ? "successful" : "failed"));
39 }
40}