this program includes 3 files>>1.Acc.java which u must run this to
execute your program 2.newUser.java 3.MenuWindow
I used the Mysql language as you wanted
so your database if you want to be same as the coding part must be like this:
Database name: Acc1>>>localhost username:shah1; password:shah;
Table:users column names: userName(varchar),password(varchar)
the files are separated by ///////////***********
1)Acc.java which you must comile and run it
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Acc extends JFrame
{
// JDBC driver, database URL, username and password
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL =
"jdbc:mysql://localhost/Acc1";//Acc1 is a database name
static final String USERNAME= "shah1";// database username
static final String PASSWORD= "shah";//database password
// default query retrieves all data from authors table
String Result = null;
Statement statement = null; // query statement
Connection connection = null; // manages connection
JTextField tf1;
Label l1,l2;
JPasswordField pf;
JButton b1,b2,newUser;
String UserName=null;
String Password=null;
int i=3;
public Acc()
{
super("Identifying");
UIManager.LookAndFeelInfo looks[];
looks=UIManager.getInstalledLookAndFeels();
try
{
UIManager.setLookAndFeel(looks[1].getClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
getContentPane().setBackground(Color.GRAY);
String s1,s2;
int count=0;
setLayout(new FlowLayout());
tf1= new JTextField(7);
pf= new JPasswordField(11);
l1=new Label("UserName:");
l2=new Label(" Password:");
add(l1);
add(tf1);
add(l2);
add(pf);
b1=new JButton(" Ok ");
b2=new JButton("Cancel");
add(b1);
add(b2);
tf1.setEditable(true);
ButtonHandler handler=new ButtonHandler();
b1.addActionListener(handler);
b2.addActionListener(handler);
newUser=new JButton("New User?");
add(newUser);
newUser.addActionListener(handler);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==b1)
{
try
{
Class.forName( JDBC_DRIVER ); // load database driver class
// establish connection to database
connection =
DriverManager.getConnection( DATABASE_URL,"shah1", "shah");
// create Statement for querying database
statement = connection.createStatement();
Result="SELECT password FROM users WHERE userName='"+tf1.getText()+"'";
// query database
String st=tf1.getText();
ResultSet resultSet = statement.executeQuery(Result);
while ( resultSet.next() )
{
if((pf.getText()!=null)&&(pf.getText()).equals(resultSet.getObject(1)))
{
String pas=resultSet.getString(1);
setVisible(false);
MenuWindow mw = new MenuWindow(st);
mw.setLocation(300,10);
mw.setDefaultCloseOperation(3);
mw.setSize(300,300);
mw.setResizable(false);
mw.setVisible(true);
}
}
}
// end try
catch ( SQLException sqlException )
{
sqlException.printStackTrace();
System.exit( 1 );
} // end catch
catch ( ClassNotFoundException classNotFound )
{
classNotFound.printStackTrace();
System.exit( 1 );
} // end catch
finally
{
try
{
statement.close();
connection.close();
} // end try
catch ( Exception exception )
{
exception.printStackTrace();
System.exit( 1 );
} // end catch
}
}
else if(event.getSource()==b2)
{
System.exit(0);
}
else if (event.getSource()==newUser)
{
newUser jd=new newUser();
jd.setLocation(400,300);
jd.setDefaultCloseOperation(2);
jd.setSize(240,150);
jd.setVisible(true);
}
}
}
public static void main(String[] args)
{
Acc ac1=new Acc();
ac1.setLocation(400,300);
ac1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ac1.setSize(180,150);
ac1.setUndecorated(true);
ac1.setVisible(true);
}
}
////////////////////////////////////////////////////////////////////////
***********************************************************************
2)MenuWindow.java which must compile and shows the user name at the top
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class MenuWindow extends JFrame
{
public MenuWindow(String st)
{
super("Any topic");
setLayout(new FlowLayout());
JLabel name=new JLabel(st);
add(name);
}
}
/////////////////////////////////////////////////////////////////////////
************************************************************************
3)newUser.java which get the new user name and password:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class newUser extends JFrame
{
Connection con;
Statement smt;
public newUser()
{
super("New User");
setLayout(new FlowLayout());
JLabel name=new JLabel("User Name: ");
add(name);
final JTextField tname=new JTextField(10);
add(tname);
JLabel pass=new JLabel("Password: ");
add(pass);
final JPasswordField tpass=new JPasswordField(15);
add(tpass);
JLabel repass=new JLabel("ReEnter pass..");
add(repass);
final JPasswordField trepass=new JPasswordField(15);
add(trepass);
JButton ok=new JButton("Ok");
add(ok);
// By pressing the ok key of new user frame the user name and
password and the type
// his accessablity will be saved in the database for later use
ok.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if((tpass.getText()).equals(trepass.getText()))
{
setVisible(false);
String str=tname.getText();
if(str.equals(""))
{
JOptionPane.showMessageDialog(newUser.this,"username and
password must be entered","Error",JOptionPane.PLAIN_MESSAGE);
setVisible(true);
}//end if
else
{
try
{
Class.forName("com.mysql.jdbc.Driver"); // load database driver class
// establish connection to database
con =
DriverManager.getConnection("jdbc:mysql://localhost/Acc1","shah1",
"shah");
// create Statement for querying database
smt = con.createStatement();
String siah="INSERT INTO users (userName, password) VALUES
('"+tname.getText()+"', '"+tpass.getText()+"')";
smt.executeUpdate(siah);
}//end try
catch ( SQLException sqlException )
{
JOptionPane.showMessageDialog(newUser.this,"This account
already exists","Error",JOptionPane.PLAIN_MESSAGE);
} // end catch
catch ( ClassNotFoundException classNotFound )
{
JOptionPane.showMessageDialog(newUser.this,"class
NotFound","DONE",JOptionPane.PLAIN_MESSAGE);
} // end catch
finally
{
try
{
smt.close();
con.close();
} // end try
catch ( Exception exception )
{
exception.printStackTrace();
System.exit( 1 );
} // end catch
}//end finally
}//end else
}//end if
else
{
setVisible(true);
JOptionPane.showMessageDialog(newUser.this,"Passwords are not
match","Error",JOptionPane.PLAIN_MESSAGE);
}//end else
}//end actionperformed for ok
}//end actionlistener for ok
);
}//end constructor
}//end class newUser |