Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: if statement incorrectly comparing strings

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    Wisconsin
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question if statement incorrectly comparing strings

    I have a bit of code that is just a simple login screen. I have a Swing GUI showing text field and a password field with some other labels and such.

    The end user enters a user and pass and once the login button is pressed, the contents of the fields are supposed to go into string variables which then my program connects to a mySQL DB and searches for a matching username, retrieves the password associated with that username and compares the SQL password with the entered string variable pass. If they match, a dialog box appears saying they entered the right information, else, a dialog box saying they entered the wrong info.

    **NOTE: I did try setting the if to (dbpass!=pass) and it will return whatever I set it to do if they do not match. ALSO, the program is retrieving the password correctly because I, as a test, set it to displayed the retrieved pass, and it is the correct one.

    I have posted the code to the whole JPanel but have removed sensitive mySQL DB login information as I can correctly connect to the DB and pull info from it.

     
    package intro;
     
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import java.awt.Font;
     
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JButton;
    import java.awt.event.ContainerAdapter;
    import java.awt.event.ContainerEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.sql.*;
     
    @SuppressWarnings("unused")
    public class Intro extends JPanel {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JTextField textField;
    	private JPasswordField upass;
     
    	/**
    	 * Create the panel.
    	 */
    	public Intro() {
    		setLayout(null);
     
    		JPanel panel = new JPanel();
    		panel.setBounds(10, 11, 580, 30);
    		add(panel);
     
    		JLabel lblWelcomeToThe = new JLabel("Welcome to the TotalBuild TMS");
    		panel.add(lblWelcomeToThe);
     
    		JLabel lblMinecraftUsername = new JLabel("Minecraft Username");
    		lblMinecraftUsername.setFont(new Font("Tahoma", Font.PLAIN, 11));
    		lblMinecraftUsername.setBounds(20, 52, 96, 14);
    		add(lblMinecraftUsername);
     
    		JLabel lblPassword = new JLabel("Password");
    		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 11));
    		lblPassword.setBounds(20, 77, 96, 14);
    		add(lblPassword);
     
    		textField = new JTextField();
    		textField.setBounds(126, 49, 120, 20);
    		add(textField);
    		textField.setColumns(10);
     
    		upass = new JPasswordField();
    		upass.setBounds(126, 74, 120, 20);
    		add(upass);
     
    		JButton btnNewButton = new JButton("Login");
    		btnNewButton.isDefaultCapable();
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				String user = textField.getText();
    				String pass = upass.getText();
    				Connection con = null;
    				String url = "jdbc:mysql://mySQL_ADDRESS:PORT/";
    				String driver = "com.mysql.jdbc.Driver";
    				String db = "DB_NAME";
    				try{
    					  Class.forName(driver);
    					  con = DriverManager.getConnection(url+db, "mySQL_USER", "mySQL_PASS");
    					  try{
    					  Statement st = con.createStatement();
    					  st.executeQuery("SELECT * FROM ustep1 WHERE user='" + user + "'");
    					  ResultSet rs = st.getResultSet ();
    					  String dbpass = "";
    					  while (rs.next ())
    					   {
    						  dbpass = rs.getString("pass");
    					   }
    					  if(dbpass==pass){
    					  String result = "Welcome, " + user + ". You entered the correct login info.";
    					  JOptionPane.showMessageDialog(null, result);
    					  } else {
    						  String result2 = "Sorry " + user + ", You entered incorrect login info." + dbpass;
    						  JOptionPane.showMessageDialog(null, result2);
    					  }
    					  }
    					  catch (SQLException s){
    					  System.err.println("SQL Error!");
    					  }
    					  }
    					  catch (Exception e){
    					  e.printStackTrace();
    					  }
     
    			}
    		});
    		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
    		btnNewButton.setBounds(30, 102, 89, 23);
    		add(btnNewButton);
     
    		JButton btnRegister = new JButton("Register");
    		btnRegister.setFont(new Font("Tahoma", Font.PLAIN, 11));
    		btnRegister.setEnabled(false);
    		btnRegister.setBounds(136, 102, 89, 23);
    		add(btnRegister);
     
    	}
     
    	private void setDefaultButton(JButton btnNewButton) {
    		// TODO Auto-generated method stub
     
    	}
     
    }
    Last edited by Pantheon8; August 14th, 2011 at 08:59 PM.


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Location
    Wisconsin
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: if statement incorrectly comparing strings

    just realized I was using "==" instead of (dbpass.equals(pass))...

    DOH!

Similar Threads

  1. Comparing Two Dates
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 27th, 2011, 02:09 PM
  2. Sorting and comparing errors.
    By tendulkarfan4life in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 12:40 AM
  3. Comparing Strings?
    By wandertheverse in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 09:32 PM
  4. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM
  5. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM