Creating A JAVA Login System With MYSQL Database Table

Share:
HOW TO CREATE A JAVA LOGIN SYSTEM WITH MySql DATABASE?
Hello Readers, in this article i am going to show you how to create a login system in java with mysql database.
So Lets Begin
  • For that first of all you need to download the platform independent mysql connector jar file.
  • After downloading jar file,create a project in Eclipse or in your favorite IDE with main method.
  • Now right click on project,1)now click on configure build path,2)click on Add External Jars,3)Now
  • find your downloaded mysql connector jar file and click on that and say apply and close.
  • Now the Referenced Libraries Directory will be created in that jar files are present that you added in last step.
  • Now in your class in main method or create your own local mehtod perform the following steps.
1)Register the driver
    Class.forName("com.mysql.jdbc.Driver");

2)Create the Connection
String url,username,password;
url="mysql:jdbc:jdbc:mysql://localhost:3306/thetechmatin";
//note:thetechmatin is database name.
username="root or your username";//if it is a localhost
password="your password";//if it localhost then keep it blank.
Connection conn=DriverManager.getConnection(url,username,password);

  • Thats all for creating connection to database.
  • Now you can perform execution of queries according to your  requirement.
 Our Purpose Is To Create A Login System.
Steps to achieve our target...
  • Create a mysql database and a table with username and password column.
  • Insert Some Users Credentials like e.g., username as virat and password as kohli.
Creating a Database:
Here my database name is thetechmatin;
-so execute the following SQL statement

create database thetechmatin;

-Now create a Table give name as login execute the below statement.
create table login
(username varchar(20),
password varchar(20)
);

-So now we have a database thetechmatin and a Table login with    column username and password.

-Now insert values in table 
INSERT INTO login(username,password) VALUES('virat','password');

-Now Follow The Below Steps
File Name : MyClass.java
import java.sql.*;
import java.io.BufferedReader;
public class MyClass{

public static void main(String[] args) throws
ClassNotFoundException, SQLException, IOException{
String url,username,password;
url="mysql:jdbc:jdbc:mysql://localhost:3306/thetechmatin";
//note:thetechmatin is database name.
  
username="root";//if it is a localhost
password="";//if it localhost then keep it blank.
Connection conn=
DriverManager.getConnection(url,username,password);
System.out.println("Connected");
//checking the connection if connected
     
//reading input from the user
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Username");
String username=read.readLine();
System.out.println("Enter Password");
String password=read.readLine();

String query="select password from login where username=? ";
PreparedStatement pst=conn.prepareStatement(query);
pst.setString(1,username());
ResultSet rs=pst.executeQuery();
    
if(rs.next()){  
   if( rs.getString(1).equalsIgnoreCase(password)){
         System.out.println("Logged In");
        //put your code according to your requirement
       } 
     else{
          System.out.println("Invalid Password");
       }
}
else{
    System.out.println("Invalid Username");
 }
  }//end of main method
}//end of class

In the above example first we registered the Driver class and then we created connection using getConnection() method of DriverManager class by passing URL, Username and Password as argument.
After that we are using Buffered Reader to take input from the user i.e., username and password and then validating them using MySql query and displaying appropriate message.
If user enter valid username and password then Logged In message will be displayed otherwise invalid username or password.

Hope It Will Help You.
Good Luck :)

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database