Results 1 to 7 of 7
  1. #1
    kiz106 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default comparing username and password fields for equality.

    Am new to java.How can I solve this small problem? Am using database based authentication to enable mi logon.Am trying to compare the username and passfield fields for equality but I dont know the best conditional operator/logic operator to use. Here is my sample code that contains a red wavy underline in the Netbeans IDE am using.

    If (userName !false) && (password !true)
    System.out.println("Invalid Password")

    Please help me out and show me the best way to do it?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The first thing is that you have to be able to know what you want to do and be able to describe it.
    Say in words what you want to achieve. Start with "if".

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kiz106 View Post
    Java Code:
    If (userName !false) && (password !true)
    In Boolean logic 'not false' is true and 'not true' is false, so you can simplify that expression a bit:

    Java Code:
    if (username true) && (password false)
    But what does that mean? You have a variable next to a boolean value; that doesn't make sense in Java; it is the same as, say:

    Java Code:
    count 42
    That doesn't make sense either. First try to put in words what you have in mind and then try to translate it to Java.

    kind regards,

    Jos

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Um, unless I'm missing something here, since your user/password data is in a db why aren't you doing this in the SELECT statement?

    There should be no need whatsoever to get the password out of the db and then do a comparison.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by kiz106 View Post
    Please help me out and show me the best way to do it?
    in order to compare a password and a username in your java-code you must hold the password and the username in your code and this is a bad idea for security reasons. usually you let enter the user his account name and the password and send it to the database for validation. so if the authentication was successful you will get an connection otherwise an exception. this means invalid account name and/or invalid password.

  6. #6
    kiz106 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default comparing username and password fields for equality

    This is my sample code.Alot is missing j2me64 but I think U can edit it out and send me a reply and please show me how i can only let the user insert there username for them to login.Am new to java and thanks for your assistance.




    package Login_form;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSet;






    public class Login extends javax.swing.JFrame {

    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;
    int curRow = 0;
    private String password;



    public Login() {
    initComponents();
    DoConnect();
    }

    public void DoConnect () {
    try {
    String driver = "org.apache.derby.jdbc.Client";
    String url="jdbc:derby://localhost:1527/sample?user=<userName>&password=<Password>";
    String uName="app";
    String uPass="app";






    stmt=con.createStatement();
    String sql="INSERT INTO APP.LOGIN values (USERNAME, PASSWORD)";
    rs=stmt.executeQuery(sql);

    if (rs.next()) {





    con=DriverManager.getConnection(url);
    stmt=con.createStatement();

    }

    }
    catch (SQLException err) {
    System.out.println(err.getMessage());
    }
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    btnUserName = new javax.swing.JTextField();
    btnPassword = new javax.swing.JPasswordField();
    btnLogin = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setTitle("UNRA-LABOUR BASED ROAD WORKS MANAGEMENT SYSTEM");

    jLabel1.setText("USERNAME");

    jLabel2.setText("PASSWORD");

    btnLogin.setText("LOGIN");
    btnLogin.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    btnLoginActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addComponent(btnLogin)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(btnPassword)
    .addComponent(btnUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))))
    .addContainerGap(40, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(btnUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(btnPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addComponent(btnLogin)
    .addContainerGap(22, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(33, 33, 33)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(87, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(60, 60, 60)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(98, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {

    String username = "Username";
    String Password = "Password";





    }

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Login().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnLogin;
    private javax.swing.JPasswordField btnPassword;
    private javax.swing.JTextField btnUserName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration

    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    1. Use code tags when posting code, otherwise you get unformatted code like yours is, which is almost impossible to read.

    2. Split out the display from the db access. Your current code is a huge blob of stuff that makes it difficult to follow what's going on, and will be hard to debug.

    3. You are doing an INSERT statement and then treating it as a query. INSERTs do not give you a result set.

Similar Threads

  1. password username and databases
    By chalo in forum JCreator
    Replies: 0
    Last Post: 12-02-2008, 09:11 AM
  2. username password verification
    By bheezee in forum JDBC
    Replies: 0
    Last Post: 11-25-2008, 07:55 PM
  3. JSTL -- Comparing two strings for equality
    By trinkets in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-12-2008, 05:39 PM
  4. Help, created a username and password box
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:21 AM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 12:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •