Login Form verification MySQL "Java"

Reply

Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Re: Login Form verification MySQL "Java"

 
0
  #11
Sep 11th, 2008
Thanks very much for the nice feedback, and solution.
I went through the code and everything seems fine, but I don't know I enter the correct username and password, but still I see the message of the Wrong password or Username?

Here is the code I did as you mentioned:

  1.  
  2. //ConnectDB.java
  3.  
  4. import com.mysql.jdbc.Connection;
  5. import java.sql.DriverManager;
  6.  
  7. class ConnectDB {
  8. private static Connection cn;
  9. public void ConnectDB(){
  10.  
  11. }
  12.  
  13. public static Connection getConnection() {
  14.  
  15. //code the returns the connection to the database:
  16. // here you write what is needed only for the connection.
  17. // ...
  18.  
  19. try {
  20. Class.forName("com.mysql.jdbc.Driver").newInstance();
  21. cn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/dictionary?"+ "user=root&password=root");
  22.  
  23. }
  24. catch (Exception ex) {
  25. // handle any errors
  26. System.out.println("SQLException: " + ex.getMessage());
  27. }
  28. return cn;
  29. }
  30. }

=============

  1.  
  2. //UserDB.java
  3.  
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8.  
  9. class UserDB {
  10. Statement stat;
  11. PreparedStatement pstat = null;
  12. ResultSet rs = null;
  13. boolean result;
  14.  
  15. public UserDB(){
  16.  
  17. }
  18.  
  19. public boolean validateUser(String username, String password) {
  20. Connection conn = ConnectDB.getConnection();
  21.  
  22. //write the query, executed, and return if the user can login or not
  23. try {
  24. pstat = conn.prepareStatement("select username,password from user where username='"+ username + "' and password = '"+password+ "'");
  25.  
  26. pstat.setString(1,username);
  27. pstat.setString(2,password);
  28. rs = pstat.executeQuery();
  29.  
  30. if(!rs.next() && rs.getRow() == 0) {
  31. result = false;
  32. }
  33. else{
  34. result = true;
  35. }
  36. }
  37. catch (Exception ex) {
  38. // handle any errors
  39. System.out.println("SQLException: " + ex.getMessage());
  40. }
  41. return result;
  42. }
  43. }


============

  1.  
  2. //LoginForm.java
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. public class LoginForm extends JFrame {
  9.  
  10. // Variables declaration
  11. private JLabel jLabel1;
  12. private JLabel jLabel2;
  13. private JTextField jTextField1;
  14. private JPasswordField jPasswordField1;
  15. private JButton jButton1;
  16. private JPanel contentPane;
  17. boolean loop = false;
  18. // End of variables declaration
  19.  
  20. public LoginForm() {
  21. super();
  22. create();
  23. this.setVisible(true);
  24. }
  25.  
  26. private void create() {
  27. jLabel1 = new JLabel();
  28. jLabel2 = new JLabel();
  29. jTextField1 = new JTextField();
  30. jPasswordField1 = new JPasswordField();
  31. jButton1 = new JButton();
  32. contentPane = (JPanel)this.getContentPane();
  33.  
  34. //
  35. // jLabel1
  36. //
  37. jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
  38. jLabel1.setForeground(new Color(0, 0, 255));
  39. jLabel1.setText("username:");
  40. //
  41. // jLabel2
  42. //
  43. jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
  44. jLabel2.setForeground(new Color(0, 0, 255));
  45. jLabel2.setText("password:");
  46. //
  47. // jTextField1
  48. //
  49. jTextField1.setForeground(new Color(0, 0, 255));
  50. jTextField1.setSelectedTextColor(new Color(0, 0, 255));
  51. jTextField1.setToolTipText("Enter your username");
  52.  
  53. //
  54. // jPasswordField1
  55. //
  56. jPasswordField1.setForeground(new Color(0, 0, 255));
  57. jPasswordField1.setToolTipText("Enter your password");
  58.  
  59. //
  60. // jButton1
  61. //
  62. jButton1.setBackground(new Color(204, 204, 204));
  63. jButton1.setForeground(new Color(0, 0, 255));
  64. jButton1.setText("Login");
  65. jButton1.addActionListener(new ActionListener() {
  66. public void actionPerformed(ActionEvent e) {
  67. jButton1_actionPerformed(e);
  68. }
  69.  
  70. });
  71.  
  72. //
  73. // contentPane
  74. //
  75. contentPane.setLayout(null);
  76. contentPane.setBorder(BorderFactory.createEtchedBorder());
  77. contentPane.setBackground(new Color(204, 204, 204));
  78. addComponent(contentPane, jLabel1, 5,10,106,18);
  79. addComponent(contentPane, jLabel2, 5,47,97,18);
  80. addComponent(contentPane, jTextField1, 110,10,183,22);
  81. addComponent(contentPane, jPasswordField1, 110,45,183,22);
  82. addComponent(contentPane, jButton1, 150,75,83,28);
  83.  
  84. //
  85. // login
  86. //
  87. this.setTitle("Login To Members Area");
  88. this.setLocation(new Point(76, 182));
  89. this.setSize(new Dimension(335, 141));
  90. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  91. this.setResizable(false);
  92. } //end of create()
  93.  
  94. /** Add Component Without a Layout Manager (Absolute Positioning) */
  95. private void addComponent(Container container,Component c,int x,int y,int width,int height) {
  96. c.setBounds(x,y,width,height);
  97. container.add(c);
  98. } //end of addComponent()
  99.  
  100. private void jButton1_actionPerformed(ActionEvent e) {
  101.  
  102. String username = new String(jTextField1.getText());
  103. String password = new String(jPasswordField1.getText());
  104.  
  105. if(username.equals("") || password.equals("")) { // If password and username is empty > Do this >>>
  106.  
  107. jButton1.setEnabled(false);
  108. JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>You must enter a username and password to login.</FONT></HTML>");
  109. JOptionPane.showMessageDialog(null,errorFields);
  110. jTextField1.setText("");
  111. jPasswordField1.setText("");
  112. jButton1.setEnabled(true);
  113. this.setVisible(true);
  114. } //end of if
  115. else {
  116.  
  117. UserDB udb=new UserDB();
  118. boolean canLogin = udb.validateUser(username, password);
  119.  
  120. do{
  121. loop = false;
  122.  
  123. if(!canLogin) {
  124. JOptionPane.showMessageDialog(null, "Wrong Username or Password, try again", "Warning !!!", JOptionPane.WARNING_MESSAGE);
  125. jTextField1.setText("");
  126. jPasswordField1.setText("");
  127. loop = true;
  128. break;
  129. } //end of if
  130. else {
  131. JOptionPane.showMessageDialog(null, "Welcome, you can use the program ...", "Welcome", JOptionPane.WARNING_MESSAGE);
  132. } //end of else
  133. } //end of do
  134. while (loop);
  135. } //end of else
  136. } //end of jButton1_actionPerformed()
  137.  
  138. public static void main(String[] args) {
  139. JFrame.setDefaultLookAndFeelDecorated(true);
  140. JDialog.setDefaultLookAndFeelDecorated(true);
  141.  
  142. try {
  143. UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  144. } //end of try
  145.  
  146. catch (Exception ex) {
  147. System.out.println("Failed loading L&F: ");
  148. System.out.println(ex);
  149. } //end of catch
  150.  
  151. new LoginForm();
  152. }; //end of main()
  153.  
  154. } //end of LoginForm

can you please have another look ?
regards,
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Login Form verification MySQL "Java"

 
0
  #12
Sep 15th, 2008
Read this post

Originally Posted by javaAddict View Post
Since you are using PreparedStatement and not the simple Statement then change this:

pstat = conn.prepareStatement("select username,password from user where username='"+ username + "' and password = '"+password+ "'"); to this:

pstat = conn.prepareStatement(
"select username,password from user where username=? and password = ?"
);
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Login Form verification MySQL "Java"

 
0
  #13
Sep 15th, 2008
Your real problem is this:
  1. public boolean validateUser(String username, String password) {
  2. Connection conn = ConnectDB.getConnection();
  3.  
  4. //write the query, executed, and return if the user can login or not
  5. try {
  6. pstat = conn.prepareStatement("select username,password from user where username='"+ username + "' and password = '"+password+ "'");
  7.  
  8. pstat.setString(1,username);
  9. pstat.setString(2,password);
  10. rs = pstat.executeQuery();
  11.  
  12. if(!rs.next() && rs.getRow() == 0) {
  13. result = false;
  14. }
  15. else{
  16. result = true;
  17. }
  18. }
  19. catch (Exception ex) {
  20. // handle any errors
  21. System.out.println("SQLException: " + ex.getMessage());
  22. }
  23. return result;
  24. }
  25. }

You catch the exception but you return the result (true or false) like nothing happened. You need to indicate to the one who called the method that something went wrong. If you simple print the error and then return the user will think that the password was wrong even though you had an exception. Try this:

  1. public boolean validateUser(String username, String password) throws Exception {
  2. Connection conn = ConnectDB.getConnection();
  3.  
  4. //write the query, executed, and return if the user can login or not
  5. try {
  6.  
  7. }
  8. catch (Exception ex) {
  9. // handle any errors
  10. System.out.println("SQLException: " + ex.getMessage());
  11. throw ex;
  12. }
  13. return result;
  14. }
  15. }

And now this:

  1. do{
  2. loop = false;
  3.  
  4. if(!canLogin) {
  5. JOptionPane.showMessageDialog(null, "Wrong Username or Password, try again", "Warning !!!", JOptionPane.WARNING_MESSAGE);
  6. jTextField1.setText("");
  7. jPasswordField1.setText("");
  8. loop = true;
  9. break;
  10. } //end of if
  11. else {
  12. JOptionPane.showMessageDialog(null, "Welcome, you can use the program ...", "Welcome", JOptionPane.WARNING_MESSAGE);
  13. } //end of else
  14. } //end of do
  15. while (loop);

If the login is wrong you set the loop=true meaning that you will repeat the while-loop but then you break, so the loop=true is meaningless, as well as the while-loop
If the login is correct you print the message and you continue (without resetting the loop=false). Again the while-loop is meaningless.

What exactly you want to do?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Login Form verification MySQL "Java"

 
0
  #14
Sep 15th, 2008
Another mistake that you made, hence the third different post so you will not confuse them is that you don't close the ResultSet, the pstm and the conn.

  1. public boolean validateUser(String username, String password) {
  2. Connection conn = ConnectDB.getConnection();
  3.  
  4. //write the query, executed, and return if the user can login or not
  5. try {
  6. pstat = conn.prepareStatement("select username,password from user
  7. where username= ? and password = ?");
  8.  
  9. pstat.setString(1,username);
  10. pstat.setString(2,password);
  11. rs = pstat.executeQuery();
  12.  
  13. if(!rs.next() && rs.getRow() == 0) {
  14. result = false;
  15. }
  16. else{
  17. result = true;
  18. }
  19. }
  20. catch (Exception ex) {
  21. // handle any errors
  22. System.out.println("SQLException: " + ex.getMessage());
  23. throw ex;
  24. } finally {
  25. if (rs!=null) rs.close();
  26. if (pstat!=null) pstat.close();
  27. if (conn !=null) conn.close();
  28. }
  29. return result;
  30. }
  31. }
Last edited by javaAddict; Sep 15th, 2008 at 5:12 pm.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Re: Login Form verification MySQL "Java"

 
0
  #15
Sep 15th, 2008
Thanks again for guiding, and also thanks for mentioning about the loop, for the moment it is useless, I will use it just, if the user enter wrong username and password more than three times, then the project exists.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 18
Reputation: viber101 is an unknown quantity at this point 
Solved Threads: 0
viber101 viber101 is offline Offline
Newbie Poster

Re: Login Form verification MySQL "Java"

 
-1
  #16
Jan 24th, 2009
follow JavaAddict instruction:

try this: erase the statement loop=true then thats it. it works for me.
regards JavaAddict))
======================================
do{
loop = false;

if(!canLogin) {
JOptionPane.showMessageDialog(null, "Wrong Username or Password, try again", "Warning !!!", JOptionPane.WARNING_MESSAGE);
tf.setText("");
pf.setText("");
//loop = true;
break;
} //end of if
else {
JOptionPane.showMessageDialog(null, "Welcome, you can use the program ...", "Welcome", JOptionPane.WARNING_MESSAGE);
} //end of else
} //end of do
while (loop);
} //end of else
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Login Form verification MySQL "Java"

 
0
  #17
Jan 24th, 2009
Thank you for repeating my code. I am sure that after months tanha hasn't solved his problem, but you were the one he was waiting for.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Re: Login Form verification MySQL "Java"

 
0
  #18
Jan 25th, 2009
Thanks dear for the replying ...

Thanks from all, sorry posting too late
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,189
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Login Form verification MySQL "Java"

 
0
  #19
Jan 25th, 2009
It would be good idea if you share solution, if you got one, and finally close this thread...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC