
i want d code for login authentication from mysql database on the same pc using swings

Hi Friend,
Try the following code:
1)LoginDemo.java:
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class LoginDemo extends JFrame{
JButton SUBMIT;
JLabel label1,label2;
final JTextField text1,text2;
LoginDemo(){
setTitle("Login Form");
setLayout(null);
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
label1.setBounds(350,100,100,20);
text1.setBounds(450,100,200,20);
label2.setBounds(350,130,100,20);
text2.setBounds(450,130,200,20);
SUBMIT.setBounds(450,160,100,20);
add(label1);
add(text1);
add(label2);
add(text2);
add(SUBMIT);
setVisible(true);
setSize(1024,768);
SUBMIT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+value1+"' and password='"+value2+"'");
int count=0;
while(rs.next()){
count++;
}
if(value1.equals("") && value2.equals("")) {
JOptionPane.showMessageDialog(null,"Enter login name or password","Error",JOptionPane.ERROR_MESSAGE);
}
else if(count>0){
NextPage page=new NextPage(value1);
page.setVisible(true);
}
else{
text1.setText("");
text2.setText("");
JOptionPane.showMessageDialog(null,"Invalid Login name or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e){}
}
});
}
public static void main(String arg[]){
new LoginDemo();
}
}
2)NextPage.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class NextPage extends JFrame
{
NextPage(String st)
{
setLayout(null);
setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
JLabel lab=new JLabel("Welcome "+st);
lab.setBounds(10,10,500,20);
add(lab);
setSize(1024, 768);
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.