ei can u please help me to do a program that accepts username and passaword using java JFrame...
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
Any beginner's java book has examples on how to create gui using javax.swing.If you don't have already such a book search the internet for tutorials. And check the java API for the following classes:
JFrame, JPanel, JTextField, JPasswordField, JButton, JLabel
And the interface ActionListener
ahilan_23 0 Unverified User
Sample Code for Login Using JFrame with MySql Server Database as Back End
Further Discussion IM ahilansoftware@gmail.com,ahilan_23@yahoo.co.in
import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.*;
import java.sql.*;
import java.io.*;
/**
* Summary description for UserVallidation
*
*/
public class UserVallidation extends JFrame {
// Variables declaration
private JLabel jLabel1;
private JLabel jLabel2;
private JTextField jTextField1;
private JPasswordField jPasswordField1;
private JButton jButton1;
private JButton jButton2;
private JPanel contentPane;
Boolean loop = false;
// End of variables declaration
public UserVallidation() {
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//
this.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent() {
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jTextField1 = new JTextField();
jPasswordField1 = new JPasswordField();
jButton1 = new JButton();
jButton2 = new JButton();
contentPane = (JPanel)this.getContentPane();
//
// jLabel1
//
jLabel1.setText("EK UserName");
//
// jLabel2
//
jLabel2.setText("EK Password");
//
// jTextField1
jTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField1_actionPerformed(e);
}
});
//
// jPasswordField1
//
jPasswordField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jPasswordField1_actionPerformed(e);
}
});
//
// jButton1
//
jButton1.setText("OK");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
//
// jButton2
//
jButton2.setText("Cancel");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
//check boxes
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 10,20,91,18);
addComponent(contentPane, jLabel2, 10,70,77,18);
addComponent(contentPane, jTextField1, 100,20,100,22);
addComponent(contentPane, jPasswordField1, 100,70,100,22);
addComponent(contentPane, jButton1, 50,100,83,28);
addComponent(contentPane, jButton2, 170,100,83,28);
// UserVallidation
//
this.setTitle("EK User Verification");
this.setLocation(new Point(390, 300));
this.setSize(new Dimension(290, 200));
jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jPasswordField1KeyPressed(evt);
}
/* public void keyTyped(java.awt.event.KeyEvent evt) {
txt_useremailKeyTyped(evt);
}*/
});
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height) {
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTextField1_actionPerformed(ActionEvent e) {
System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jPasswordField1_actionPerformed(ActionEvent e) {
System.out.println("\njPasswordField1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jButton1_actionPerformed(ActionEvent e) {
login();
System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
}
//action listener
private void jPasswordField1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if(evt.getKeyCode() == 10) {
login();
}
}
public void login() {
String username=null;
String password=null;
String dburl=null;
String dburl1=null;
String dburl2=null;
DataInputStream dis=null;
username = new String(jTextField1.getText());
password = new String(jPasswordField1.getPassword());
if((username.equals(""))&&(password.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the UserName and Password","User Verification",1);
new UserVallidation();
} else if((username.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the UserName ","User Verification",1);
new UserVallidation();
} else if((password.equals(""))) {
JOptionPane.showMessageDialog(this,"Enter the Password ","User Verification",1);
new UserVallidation();
} else {
try {
dburl = "jdbc:mysql://127.0.0.1:3306/"
dburl1 = "root";
dburl2="admin";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(dburl,dburl1,dburl2);
Statement stat=con.createStatement();
System.out.println("connection opened");
PreparedStatement pstmt1=null;
ResultSet rs=null;
do{
loop = false;
username = new String(jTextField1.getText());
password = new String(jPasswordField1.getPassword());
pstmt1=con.prepareStatement("select username,password from TBLEK_REGISTRATION where username=? and password=?");
pstmt1.setString(1,username);
pstmt1.setString(2,password);
rs=pstmt1.executeQuery();
if(!rs.next() && rs.getRow() == 0) {
JOptionPane.showMessageDialog(this,"Login Failed. Please try again!");
jTextField1.setText("");
jPasswordField1.setText("");
loop = true;
new UserVallidation();
break;
}else {
username = rs.getString("username");
password=rs.getString("password");
this.setVisible(false);
}
}while (loop);
}catch(Exception Ex) {
System.out.println("Exception arises"+Ex);
}
}
this.setEnabled(false);
this.setVisible(false);
}
private void jButton2_actionPerformed(ActionEvent e) {
System.exit(1);
System.out.println("\njButton2_actionPerformed(ActionEvent e) called.");
}
}
Edited by Dani because: Formatting fixed
orko 36 Junior Poster
It is not encouraged to post such straight forward solution here. This forum definitely discourage the "copy-paste" manner.
luckycharm 0 Newbie Poster
i really don't understand why..
luckycharm 0 Newbie Poster
a java programming that will enter a unique username and pasword..
victor.a.magtangob 0 Newbie Poster
Post deleted - start a new topic please
Edited by JamesCherrill because: Don't hijack topics, start you own new topic.
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question, and post your code to show what you have done so far.
fliponymous 0 Newbie Poster
We're happy to help you, but we're not here to do all the work, because you simply don't learn anything that way.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.