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 */;


  




No comments:

Post a Comment