User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,575 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,497 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting

Login Form verification MySQL "Java"

Join Date: Dec 2007
Location: Greece
Posts: 537
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 57
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: Login Form verification MySQL "Java"

  #10  
Apr 22nd, 2008
I don't know if you are going to receive this tanha, but I am going to say it anyway.
What you have tried to do is wrong from the design point of view. Even if it compiles and does exactly what you want it is not how it should be done: You cannot have you entire code with different functionalities in one method. The gui you wrote should be used to do things only a gui would do. Meaning that shouldn't implement code that connects to a database, runs the query, gets the result and then perform some actions with the result inside the actionPerformed. A better way to do this (I am not saying it is the best way but it is easier and SIMPLER) is the following: (Some details are omitted in order to reduce the code)

Have a class that only returns the connection:
class ConnectDB {
  public static Connection getConnection() {
     //code the returns the connection to the database:
    // here you write what is needed only for the connection.
   //  ... 
     conn = DriverManager.getConnection (dbUrl, dbUser, dbPass);
   //  .. ..  ...
 return conn;
  }
}

Then you have another class in which you run only the queries. One method as an example
class UserDB {
  public boolean validateUser(String user, String pass) {
    Connection conn = ConnectDB.getConnection();
    //write the query, executed, and return if the user can login or not
    return false;
  }
}
And finally you call the above inside your gui:
String user=textUser.getText();
String pass=textPass.getText();
UserDB udb=new UserDB();
boolean canLogin = udb.validateUser(user, pass);

What you gain from this:
An easy to read and maintain code. You have classes that do specific things, not one large method with everything inside. You can call them any time you want from wherever you want.
What if you wanted to go to another frame after the login and again you wanted to run a query. With your way you would have to write again the same code for opening connection and running the query and doing stuff with the results.
With my way all you have to do is call one method from the UserDB class. If you want you can put all your queries in one class or you can categorize them in classes:
In the class UserDB you could have methods for login inserting new user, updating and you can call them from wherever you want without having to write any extra code.

Now think this. what happens if the database's url changes or some other property changes. With your code you will have to go to all the places where you open the connection in order to change your code. With my way all you have to do is go to ONE class where you open the connection and you wouldn't have to change anything else.
Last edited by javaAddict : Apr 22nd, 2008 at 7:25 am.
I AM the 12th CYLON
Reply With Quote  
All times are GMT -4. The time now is 7:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC