0

I have not found a way to return if a connection attempt was successful or not. I want a user to log on one Jframe and, if successful, switch to another Jframe. I provided the code I have so far. Any information to help with this welcomed. Thanks in advance.

private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    try {
        // TODO add your handling code here:
        ConnectDatabase connect = new ConnectDatabase();
        String output = new String(passwordField.getPassword());
        connect.getDBConnection(usernameField.getText(),output);
        //this.dispose();
        //Main main = new Main();
        //main.setVisible(true);
    } catch (Exception ex) {
        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        errorLabel.setText("Incorrect username/password");
    }
}  
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
asd
  • 47
  • 8
  • don't switch JFrames, to switch JPanels with JFrame.pack() by using CardLayout – mKorbel Sep 18 '14 at 03:59
  • 1
    Have a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) and [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for some better ideas – MadProgrammer Sep 18 '14 at 04:05
  • 1
    Also consider using a dialog instead...[How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer Sep 18 '14 at 04:06
  • Those are some very good points. The login screen is the first thing the program should show. I thought having a seperate JFrame was the correct idea, but I might have been wrong. – asd Sep 18 '14 at 04:11
  • Personally, I would have the main application come up. If it detects that there is available connection, it would open the login dialog. But you could present the login dialog first. There is some reassurance to the user to see the application launch before the login dialog is presented, but that's just me – MadProgrammer Sep 18 '14 at 04:16
  • you can dispose and start new frame after login success , you need to check login success and put specific code within that and dispose the current frame – mussdroid Sep 18 '14 at 06:30

3 Answers3

1

I have not found a way to return if a connection attempt was successful or not. I want a user to log on one Jframe and, if successful, switch to another Jframe

I think you're tackling the problem around the wrong way...

To start with, see The Use of Multiple JFrames, Good/Bad Practice?

From you main UI, you should present the user with a modal dialog of some kind. This would prompt the user for connection details and, when they select "connect" (or what ever button you have), would attempt to create the connection.

If the connection failed, it would display an error message to the user and wait (not closing the dialog, so the user can try again).

If successful, you would close the dialog and allow the caller to retrieve the details from the dialog.

See How to Make Dialogs for more details.

Another idea might be use a CardLayout. You would provide a "login" or "connection" panel, once the connection was established, you would switch screens to whatever came next.

See How to Use CardLayout for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Try this:

   try{
      //connection statements
    }catch(Exception e){
      //control come here if connection fails.
      // you can do your work in this block.(The work which you want to do after connection failure)
    }
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
0

The following code may help you

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class Login extends JFrame implements ActionListener{

JTextField t1_uname; 
JPasswordField t2_pswd;
JButton btn_lgn,btn_clr;
public Login()
{
       setLayout(null); 
       setTitle("LOGIN");
       setSize(830,720);
       setLocation(300,8);

       JPanel p1=new JPanel();
       p1.setLayout(null);
       p1.setBounds(0,0,830,720);
       p1.setBackground(Color.white);
       add(p1);

       JLabel l1=new JLabel("USERNAME :");
       l1.setBounds(140,230,130,20);
       p1.add(l1);

       t1_uname=new JTextField();
       t1_uname.setBounds(250,230,130,20);
       p1.add(t1_uname);

       JLabel l2=new JLabel("PASSWORD :");
       l2.setBounds(140,270,130,20);
       p1.add(l2);

       t2_pswd=new JPasswordField();
       t2_pswd.setBounds(250,270,130,20);
       p1.add(t2_pswd);

       btn_lgn=new JButton("LOGIN");
       btn_lgn.setBounds(250,310,100,20);
       btn_lgn.addActionListener(this);
       p1.add(btn_lgn);

       btn_clr=new JButton("CLEAR");
       btn_clr.setBounds(360,310,100,20);
       btn_clr.addActionListener(this);
       p1.add(btn_clr);

       JLabel background=new JLabel(new ImageIcon("images/hd_login.jpg"));
       background.setBounds(130,80,830,720);
       p1.add(background);

       setVisible(true);

 }
  public static void main(String[] args) {
    Login obj=new Login();
 }  

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==btn_lgn)
    {
        String uname=t1_uname.getText();
        String pswd=t2_pswd.getText();
        Dbconnection obj=new Dbconnection();
        int k=obj.loginCheck(uname, pswd);
        if(k==1)
        {
            AdminHome obj_AdminHome=new AdminHome();
            setVisible(false);
        }
        else
        {
            JOptionPane.showMessageDialog(null,"WRONG USERNAMR OR PASSWORD");
        }


    }
    else if(e.getSource()==btn_clr)
    {
        t1_uname.setText("");
       t2_pswd.setText("");
    }
   }
 }

Method in DbConnection, this is just a method , you have to create connection to database,

 import java.sql.*;
 import java.util.*;
 public class Dbconnection {

Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;

public Dbconnection()
{
    try{

            Class.forName("com.mysql.jdbc.Driver");
                 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME","root","root");   
        }
        catch(Exception e)
        {
            System.out.println("Error in connection"+e);
        }
}


     public int loginCheck(String username,String password)
  {
      int flag=0;
      try{
          ps=con.prepareStatement("select * from tbl_login where username=? and password=? ");
            ps.setString(1,username);
            ps.setString(2,password);

            rs=ps.executeQuery();
            while(rs.next())
            {
                flag=1;
            }

      }
      catch(Exception e)
      {
          System.out.println("Error in loginCheck"+e);
      }
      return flag;
  }
 }

Here, AdminHome page is new JFrame, that you have to create, the all components created in CONSTRUCTOR of the AdminHome class, So, creating the object of that class will show the JFrame and its components

Dbconnection is a class that has the connection to database and method for login check

Anptk
  • 1,125
  • 2
  • 17
  • 28