Javatpoint Logo

how to create a simple login page?

By: ravind*** On: Tue Mar 05 17:40:18 IST 2013     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
creating simple login page using core javaUp0Down

 

Hi Friend,

Try the following code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Form extends JFrame implements ActionListener{
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1;
final JPasswordField text2;
Form(){
label1 = new JLabel();
label1.setText("UserName:");
text1 = new JTextField(15);

label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("Login");
panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("FORM");
}
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
String user1="";
String pass1="";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM login where username='"+value1+"' && password='"+value2+"'");
while (res.next()) {
user1 = res.getString("username");
pass1 = res.getString("password");
}
if (value1.equals(user1) && value2.equals(pass1)) {
JOptionPane.showMessageDialog(this,"correct");
}
else{
JOptionPane.showMessageDialog(this,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
class LoginDemo{
public static void main(String arg[]) {
try {
Form frame=new Form();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}

Thanks
Image Created0Down

By: [email protected] On: Tue Mar 05 17:41:29 IST 2013 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :5Yes21No
 
u can try this one too

To create a Login Form, we have used two class files:
1) NextPage.java
2) LoginDemo.java

Here is the code of NextPage.java
import javax.swing.*;
import java.awt.*;

class NextPage extends JFrame
{
NextPage()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
setSize(400, 200);
}
}


Here is the code of LoginDemo.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Login extends JFrame implements ActionListener
{
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);

label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);

SUBMIT=new JButton("SUBMIT");

panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
}
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("roseindia") && value2.equals("roseindia")) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
Image Created0Down

By: [email protected] On: Wed Mar 06 11:37:18 IST 2013 Question Reputation0 Answer Reputation392 Belt Series Points0 392User Image
Are You Satisfied :7Yes7No