2

I'm trying to create a GUI program, simple LOGIN maintenance. I have 4 gui components namely: jlabel, jtextfield, jpasswordfield and jbutton. so far, this is my code:

import java.awt.Container;
import java.awt.FlowLayout;
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.*;


public class BankGui extends JApplet implements ActionListener{

// GUI components
JLabel lblUser, lblPass;
JTextField txtUser;
JPasswordField txtPass;
JButton btnOk, btnClear;

// connections to MYSQL
private static Connection connection = null;
private static Statement statement = null;
private static ResultSet resultSet = null;
//public static Scanner in = new Scanner(System.in);

public void init(){
    Container c = getContentPane();
    c.setLayout( new FlowLayout() );

    lblUser = new JLabel( "Username: " );
    c.add( lblUser );
    txtUser = new JTextField( 10 );
    c.add( txtUser );

    lblPass = new JLabel( "Password:" );
    c.add( lblPass );
    txtPass = new JPasswordField( 10 );
    c.add( txtPass );

    btnOk = new JButton( "OK" );
    btnOk.addActionListener( this );
    c.add( btnOk );
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if( btnOk ){

    }
}

public void connect(){

    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/USERS", "root", "root");
        statement = connection.createStatement();
        resultSet = statement
                .executeQuery("SELECT lname, fname FROM employees");

    } catch (Exception e) {
        e.printStackTrace();
    }

}


}

But I'm stuck in the method actionperformed and my connect method. I don't know what to put there to validate if that person who logged in is an authorized user or not.

1
  • For anyone in the future, here's a simple tutorial on how to create a simple GUI login screen using java and mysql Commented Jan 4, 2013 at 11:03

2 Answers 2

2

You can't perform SQL operation with Applet please change it to Java application (extends the Frame/JFrame). Read this article - What Applets Can and Cannot Do.

You have to write SELECT statement with WHERE clause.

String sql="SELECT userName FROM employees WHERE UserName=? and Password=?";

EDIT:

Register the JDBC driver in static block

 static {
   try{
       Class.forName("com.mysql.jdbc.Driver");
   }catch(Exception ex) { 
       System.err.println(ex);
   }
 }

and write code in actionPerformed method:

PreparedStatement stmt=null;
boolean isFound=false;
try{
   connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/USERS", "root", "root");
   String sql="SELECT userName FROM employees WHERE UserName=? and Password=?";
   stmt=connection.prepareStatement(sql);
   stmt.setString(1,txtUser.getText());
   stmt.setString(2,txtPass.getPassword());             
   resultSet=stmt.executeQuery();
   if(resultSet.next()){
     isFound=true;
   }
  //     
}catch(SQLException ex){
   System.err.println(ex);
}finally{
  if(stmt!=null){
      try{ 
         stmt.close();
      }catch(Exception ex) { /* */ }
  }
 if(connection!=null){
      try{ 
         connection.close();
      }catch(Exception ex) { /* */ }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

am I to put that string sql in my connect method or actionperformed?
I already changed it to JFrame, how am I going to display my textfields and button?
0

Change your code to this

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getActionCommand().equals("OK")){
                     if(connect(txtUser.getText(), txtPass.getText())) {
                                // Valid
                     }
                     else {
                           //Invalid
                     }

            }
        }

        public boolean connect(String usr, String pwd){

            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/USERS", "root", "root");
                 PreparedStatement ps= connection.prepareStatement("SELECT lname, fname FROM employees where username=? and password =?");
                 ps.setString(1,usr);
                 ps.setString(2,pwd);

                resultSet = ps.executeQuery();

              if(resultSet.next()) {
                       return true;
              }

            } catch (Exception e) {
                e.printStackTrace();
            }

    return false;
        }

10 Comments

Be careful. You're wide open for SQL attacks without using PrepareStatement.
Hi thanks for the reply. I got an error in if ( btnOk ) . my btnOk is jbutton. not boolean
@Lion What am I going to do to prevent sql attacks?
You should always use parameterized query with the help of PrepareStatement that will prevent you SQL query from SQL injection like SELECT lname, fname FROM employees WHERE UserName=? and Password=?.
check above changed code, also use PrepareStatement to execute select query to get rid from SQL Injection.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.