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
Class.forName("com.mysql.jdbc.Driver");
2)Create the Connection
Steps to achieve our target...
Here my database name is thetechmatin;
-so execute the following SQL statement
-Now create a Table give name as login execute the below statement.
-So now we have a database thetechmatin and a Table login with column username and password.
-Now insert values in table
-Now Follow The Below Steps
File Name : MyClass.java
public static void main(String[] args) throws
String query="select password from login where username=? ";
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 :)
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.
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.
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.
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