Advertisement
k0z31y4

myapp.java

May 21st, 2025
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | Source Code | 0 0
  1. import java.sql.*;
  2.  
  3. public 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement