Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class LoginService {
- private static final String DB_URL = "jdbc:mysql://localhost:3306/users?user=root&password=secret123";
- public boolean authenticate(String username, String password) {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- conn = DriverManager.getConnection(DB_URL);
- stmt = conn.createStatement();
- String query = "SELECT * FROM users WHERE username = '" + username +
- "' AND password = '" + password + "'";
- rs = stmt.executeQuery(query);
- if (rs.next()) {
- return true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (rs != null) rs.close();
- if (stmt != null) stmt.close();
- if (conn != null) conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return false;
- }
- public static void main(String[] args) {
- LoginService service = new LoginService();
- boolean result = service.authenticate(args[0], args[1]);
- System.out.println("Authentication " + (result ? "successful" : "failed"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement