First of all set your Database:
STEP 1: goto your contorl panal
->Adminstrative tool
->Data Source (ODBC)
STEP 2: Add new Microsoft Access driver (*.mdb,*.accdb)
->select your database from pressing select button
->Databse name as login1
->then ok and ok
enjoy....!!! below code
cretae java file compile it ....!!!!
------------------------------------------------------------------------------------------------------------- /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingtest;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
*
* @author #Dx Panchal
*/
public class SwingTest extends JFrame implements ActionListener{
Container c=getContentPane();
private JButton btnlogin,btncancle;
private JLabel lbluser,lblpswd;
private JTextField txtuser;
private JPasswordField txtpswd;
public SwingTest()
{
super("Login");
this.setSize(400,200);
this.setLayout(null);
this.setResizable(false);
this.setLocation(45,45);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
btnlogin=new JButton("LOGIN");
btncancle=new JButton("CANCLE");
lbluser=new JLabel("USER NAME:");
lblpswd=new JLabel("PASSWORD :");
txtuser=new JTextField();
txtpswd=new JPasswordField();
lbluser.setBounds(50, 40, 140, 25);
txtuser.setBounds(150, 40, 130, 25);
lblpswd.setBounds(50, 80, 140, 25);
txtpswd.setBounds(150,80,130,25);
btnlogin.setBounds(150, 120, 100, 25);
btncancle.setBounds(250, 120, 100, 25);
btnlogin.addActionListener(this);
btncancle.addActionListener(this);
this.add(btnlogin);
this.add(btncancle);
this.add(lbluser);
this.add(lblpswd);
this.add(txtuser);
this.add(txtpswd);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnlogin)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:login1");
try
{
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("SELECT *FROM login WHERE username='"+
txtuser.getText()+"'AND password='"+txtpswd.getText()+"'");
if(rs.next())
{
if(rs.getString(2).equals(txtuser.getText())&&rs.getString(3).equals(txtpswd.getText()))
{
JOptionPane.showMessageDialog(null,"Sucessfully Login","Correct..!!!",JOptionPane.OK_OPTION);
}
}
else
{ JOptionPane.showMessageDialog(null,"Invalid USERNAME and PASSWORD","Invalid",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception ex1)
{
JOptionPane.showMessageDialog(null, "Invalid username or password", "Invalid", JOptionPane.ERROR_MESSAGE);
txtuser.setText("");
txtpswd.setText("");
}
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "Unable to connect to the database", "Connection error", JOptionPane.ERROR_MESSAGE);
}
}
if(e.getSource()==btncancle)
{
txtuser.setText("");
txtpswd.setText("");
}
}
public static void main(String[] args) {
new SwingTest().setVisible(true);
}
}
No comments:
Post a Comment