2 Replies - 7799 Views - Last Post: 14 September 2009 - 01:26 PM Rate Topic: -----

#1 tobenna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 09

Java Database Login

Posted 14 September 2009 - 09:19 AM

I am having a problem with a java database login code. The essence of this code is for one to be able to use it to login into an application which I am developing. Now the code does not report any errors. I try to use it to login, I type in the incorrect username and password, it does not login (good), but when I try to use the correct username and password, it still would not login(bad). The database retrieves the correct information, but I do not know where the problem is coming from.
public void login()
	{
		 try{
			 String username = usernameField.getText();
			 String password = new String(passwordField.getPassword());
			 statement = connection.createStatement();
			 resultSet = statement.executeQuery(
					"SELECT * FROM login");
			 ResultSetMetaData metaData = resultSet.getMetaData();
			 int numberofColumns = metaData.getColumnCount();
			 while(resultSet.next())
			 {
				 String user = resultSet.getString(1);
				 String pass = resultSet.getString(2);
				 if((username == user)&&(password == pass))
				 {
					 jLabel1.setText("Correct Login");
				 }
				 else
				 {
					 jLabel1.setText("Incorrect Login");
				 }
			 }
		 }
		 catch(SQLException exception)
		 {
			 exception.printStackTrace();
		 }
	}
public void db_connect()
	{
		try{
			Class.forName(DRIVER);
			connection = 
					DriverManager.getConnection(DATABASE_URL, "tobenna","tobenna");
		}
		catch(ClassNotFoundException exception)
		{
			exception.printStackTrace();
		}
		catch(SQLException exception)
		{
			exception.printStackTrace();
		}
	}
	static final String DRIVER = "com.mysql.jdbc.Driver";
	static final String DATABASE_URL = "jdbc:mysql://localhost/sms";
	Connection connection = null;
	Statement statement = null;
	ResultSet resultSet = null;



The database is an sql database. The table has two columns. The first column on the table is for the username, the second is for the password, and it is not encoded.
Other outside variables:

Is This A Good Question/Topic? 0
  • +

Replies To: Java Database Login

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Java Database Login

Posted 14 September 2009 - 09:56 AM

Try the following SQL Query:

String x = "SELECT password
FROM login
WHERE username = " + username



This way, all you have to do is verify the password. Hope this helps some. Good luck!
Was This Post Helpful? 0
  • +
  • -

#3 tobenna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 09

Re: Java Database Login

Posted 14 September 2009 - 01:26 PM

View Postmacosxnerd101, on 14 Sep, 2009 - 08:56 AM, said:

Try the following SQL Query:

String x = "SELECT password
FROM login
WHERE username = " + username



This way, all you have to do is verify the password. Hope this helps some. Good luck!


Thanks, but its still the same nothing has changed. The problem is still there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1