Sunday, April 8, 2018

THREE ATTEMPTS LOGIN SYSTEM IN JAVA

A very simple program that will ask the user to give username and password it will count the number of attempts of the user maximum of three attempts using Java as my programming language.

I am currently accepting programming, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.


Program Listing


Pass.java

package login;

import java.util.Scanner;

/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * April 6, 2018  Friday
 * Bacolod City, Negros Occidental
 */

public class Pass {
    
   
   
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
       String user,pass;
        
       int attempts=0;
        
         System.out.println("===== THREE ATTEMPTS LOGIN SYSTEM IN JAVA =====");
         System.out.println("\n");
        do {
           
            System.out.print("Enter User Name : ");
            user = scanner.next();
            
            System.out.print("Enter Password :  ");
            pass = scanner.next();
            
              attempts++;
            
            if (!user.equals("user")  && !pass.equals("user") && attempts !=3){
                System.out.println("\n");
                System.out.print("Wrong Username or Password Attempt No " + attempts + ".");
                System.out.println("\n\n");
            }
            else {
                
                break;
                
            }
            
           
            
        } while (!user.equals("user")  && !pass.equals("user") && attempts !=3);
                
             if (attempts==4) {
                 System.out.println("After three attempts");
                 System.out.println("Access Denied to the system.");
               
             }
             else {
                 System.out.println("\n\n");
                 System.out.println("Welcome to the System " + user);
                 System.out.println("\n\n");
             } 
      
          
          
             scanner.close();
    
     }
}






Thursday, April 5, 2018

List of Persons Using ArrayList in Java

I am currently studying data structures in Java one of the basic data structure in ArrayList. What does the program will do is to list down all the name of the person that we declare in our program.

I am currently accepting programming, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Sample Program Output


Program Listing

ArrayCollection.java

    import java.util.*;  
    
class ArrayCollection{  

       public static void main(String args[])
    {  
ArrayList <String> list= new ArrayList <String>();
    list.add("Jake R. Pomperada");
                list.add("Lydia R. Pomperada");  
list.add("Virgilio V. Pomperada");  
                list.add("Ma. Junallie F. Pomperada");  
                list.add("Jacob Samuel F. Pomperada");  
list.add("Julianna Rae F. Pomperada");  
list.add("Felipe C. Fuentebella Jr.");  
list.add("Salvacion E. Fuentebella ");  
              
      Iterator loop=list.iterator();  
      
  System.out.println("\n");
  System.out.print("List of Persons Using ArrayList");
  System.out.println("\n");
  while(loop.hasNext()){  
       System.out.println(loop.next());  
      }  
  System.out.println("\n");
  System.out.println("End of Program");
  System.out.println("\n\n");
     }  
    }  



Wednesday, April 4, 2018

Login System with User Name Display in Java JDBC and MySQL

In this article I would like to share with you guys a database login system that I wrote in Java JDBC and MySQL as my database. What does the program will do is to ask the user to give the username and password if the username and password is correct it will display a welcome message to the screen including the complete name of the user. The user complete name the record will be fetched directly to our database. If the username and password is invalid our program will continue to ask the user until the right username and password is being given by our user. I hope you will like my work. I try to make the code shorter and easy to understand. 

I am currently accepting programming, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.





Sample Program Output



Program Listing

Login.java


import java.io.Console;
import java.sql.*;

/*
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * April 4, 2018  Wednesday
 * Bacolod City, Negros Occidental
 */

    // Compile First the Login.java   
    // To Run
    // java -cp .;mysql-connector-java-5.1.46.jar Login
    // need to have mysql library same directory as the Login.java


public class Login {
    
static Connection conn;

   static void closeConnection() {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

void display() {
Console console = System.console(); 
console.printf("\n");
    console.printf("===== LOGIN SYSTEM IN JAVA JDBC AND MYSQL =====");
    console.printf("\n\n");
        console.printf("Enter Username : ");
        String userName = console.readLine();
        char passwordArray[] = console.readPassword("Enter Password: ");
        String passWord = new String(passwordArray);
    
            String selectSQL = "select * from user where username = ? and password = ?";
 
          try {

     conn = DriverManager.getConnection("jdbc:mysql://localhost/credentials","root","");
            PreparedStatement ps = conn.prepareStatement(selectSQL);
            ps.setString(1, userName);
            ps.setString(2, passWord);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
  String lastname = rs.getString(5);
                           String firstname = rs.getString(4);
              System.out.println("\n");
  System.out.println("Welcome to the System Mr/Ms. " 
                                  + firstname.toUpperCase() 
                                  + " " + lastname.toUpperCase() + ".");
  System.out.println("\n");   
}
else
{
    System.out.println("\n");
            System.out.println("Intruder Detected.");
System.out.println("\n");
display();
}
       
       closeConnection();
             
              
        }  catch (SQLException ex) {
            ex.printStackTrace();
            
    }

}
public static void main(String[ ] args) {
               
      Login  Start = new Login();
  Start.display();
  }
    
}    


  
user.sql

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 04, 2018 at 02:29 PM
-- Server version: 10.1.28-MariaDB
-- PHP Version: 7.1.11

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `credentials`
--

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `lastname` varchar(200) NOT NULL,
  `firstname` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`id`, `username`, `password`, `lastname`, `firstname`) VALUES
(1, 'test', 'test', 'Allie', 'Pomperada'),
(2, 'admin', 'admin', 'Iya', 'Pomperada'),
(3, 'jake', 'jake', 'Jake', 'Pomperada'),
(4, 'jacob', 'jacob', 'Jacob', 'Pomperada');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `user`
--
ALTER TABLE `user`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `user`
--
ALTER TABLE `user`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


  




Tuesday, April 3, 2018

Addition of Three Numbers Using Struts

This will be my first Java Apache Struts 1 web application that I wrote a very simple program to display the sum of three numbers on the screen. I hope you will like my work. Thank you.


I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Sample Program Output



Program Listing

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>Add.jsp</welcome-file>
    </welcome-file-list>
</web-app>

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/Addition"/>

struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">


<struts-config>
    <form-beans>
    
    </form-beans>
    
    <global-exceptions>
    
    </global-exceptions>

    <global-forwards>
        <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>

    <action-mappings>
       <!-- <action path="/Welcome" forward="/welcomeStruts.jsp"/>-->
       <action path="/Add" forward="/Add.jsp"/>
    </action-mappings>
    
    <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

    <message-resources parameter="com/myapp/struts/ApplicationResource"/>    
    
    <!-- ========================= Tiles plugin ===============================-->
    <!--
    This plugin initialize Tiles definition factory. This later can takes some
    parameters explained here after. The plugin first read parameters from
    web.xml, thenoverload them with parameters defined here. All parameters
    are optional.
    The plugin should be declared in each struts-config file.
    - definitions-config: (optional)
    Specify configuration file names. There can be several comma
    separated file names (default: ?? )
    - moduleAware: (optional - struts1.1)
    Specify if the Tiles definition factory is module aware. If true
    (default), there will be one factory for each Struts module.
    If false, there will be one common factory for all module. In this
    later case, it is still needed to declare one plugin per module.
    The factory will be initialized with parameters found in the first
    initialized plugin (generally the one associated with the default
    module).
    true : One factory per module. (default)
    false : one single shared factory for all modules
    - definitions-parser-validate: (optional)
    Specify if xml parser should validate the Tiles configuration file.
    true : validate. DTD should be specified in file header (default)
    false : no validation

    Paths found in Tiles definitions are relative to the main context.
    -->
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />      
        <set-property property="moduleAware" value="true" />
    </plug-in>
    
    <!-- ========================= Validator plugin ================================= -->
    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property
            property="pathnames"
            value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>
  
</struts-config>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>Add.jsp</welcome-file>
    </welcome-file-list>
</web-app>

index.jsp


<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<jsp:forward page="Add.do"/>

Add.jsp

<%-- 
    Document   : Add
    Created on : Apr 2, 2018, 11:27:07 PM
    Author     : Jake Pomperada
--%>
<%@ page language="java"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <style>
        body {
        font-family:arial;
        font-size:16px;
        font-weight: bold;
        }
       </style>
    <body>
        <h3>Addition of Three Numbers Using Struts </h3>
        <%
 int a=5;
 int b=2;
 int c=1;

 int result=a+b+c;
  
 out.print("Created By Mr. Jake R. Pomperada");
 out.print("<br><br><br>");
 out.print("The sum of " + a + ","+ b + " and " + c + " is " + result + "."); 
%>
    </body>
</html>