Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 3.26 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. JSF Mysql Login. Problems getting data from mysql
  2. <?xml version='1.0' encoding='UTF-8' ?>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    
  4.  /TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  <html xmlns="http://www.w3.org/1999/xhtml"
  6.   xmlns:h="http://java.sun.com/jsf/html">
  7.  
  8.  
  9.    <head><title>JSF Login</title></head>
  10.  <body>
  11.      <h1>Login</h1>
  12.  <h:form>
  13. <table>
  14.  <tr>
  15. <td><h:outputText value="Username: " /></td>
  16. <td><h:inputText id="loginname"
  17.  value="#{login.userName}" />
  18.  </td>
  19. </tr>
  20. <tr>
  21. <td><h:outputText value="Password: " /></td>
  22. <td><h:inputSecret id="password"
  23. value="#{login.password}" />
  24. </td>
  25. </tr>
  26. <tr>
  27. <td> </td>
  28. <td><h:commandButton value="Login"
  29. action="#{login.checkLogin}"/>
  30. </td>
  31. </tr>
  32. </table>
  33.    <h:outputLabel value="#{login.label1}" />
  34. </h:form>
  35. </body>
  36. </html>
  37.        
  38. package login;
  39.  import javax.faces.bean.ManagedBean;
  40.  import javax.faces.bean.SessionScoped;
  41.  import java.sql.*;
  42.  
  43.  
  44.  @ManagedBean(name="login")
  45.  @SessionScoped
  46.  public class loginBean {
  47.  private String userName;
  48.  private String password;
  49.  private String label1;
  50.  private String dbpwd;
  51.  private String dbusername;
  52.  
  53.  private static int numOfAttempts = 0;
  54. /** Creates a new instance of loginBean */
  55. public loginBean() {
  56. }
  57.  
  58. /**
  59.  * @return the userName
  60.  */
  61. public String getUserName() {
  62.     return userName;
  63. }
  64.  
  65. /**
  66.  * @param userName the userName to set
  67.  */
  68. public void setUserName(String userName) {
  69.     this.userName = userName;
  70. }
  71.  
  72. /**
  73.  * @return the password
  74.  */
  75. public String getPassword() {
  76.     return password;
  77. }
  78.  
  79. /**
  80.  * @param password the password to set
  81.  */
  82. public void setPassword(String password) {
  83.     this.password = password;
  84. }
  85.  
  86. /**
  87.  * @return the label1
  88.  */
  89. public String getLabel1() {
  90.     return label1;
  91. }
  92.  
  93. /**
  94.  * @param label1 the label1 to set
  95.  */
  96. public void setLabel1(String label1) {
  97.     this.label1 = label1;
  98. }
  99.  
  100.         Connection con;
  101.     Statement ps;
  102.     ResultSet rs;
  103.     String SQL_Str;
  104.  
  105.     public void dbData(String UName)
  106.     {
  107.         try
  108.         {
  109.             Class.forName("com.mysql.jdbc.Driver");
  110.             con = DriverManager.getConnection("jdbc:mysql://localhost:3306  
  111.                 /securelogin","root","root");
  112.             ps = con.createStatement();
  113.             SQL_Str="Select * from tblusers where tbluserName =('" + UName +"')";
  114.             rs=ps.executeQuery(SQL_Str);
  115.             rs.next();
  116.             dbusername=rs.getString("tbluserName");
  117.             dbpwd=rs.getString("txtPassword");
  118.         }
  119.         catch(Exception ex)
  120.         {
  121.             ex.printStackTrace();
  122.             System.out.println("Exception Occur :" + ex);
  123.         }
  124.     }
  125.  
  126.  
  127.  public String checkLogin()
  128.  {
  129.       dbData(userName);
  130.      if (userName.equals(dbusername) && password.equals(dbpwd))
  131.     {
  132.         this.setLabel1("Login Success");
  133.         return "loginsuccess";
  134.     }
  135.     else
  136.     {
  137.         numOfAttempts++;
  138.         if (numOfAttempts >= 3)
  139.         {
  140.         this.setLabel1("Account Locked");
  141.         return "loginlocked";
  142.         }
  143.         else
  144.         {
  145.             this.setLabel1("Login Failure" + numOfAttempts + dbusername + dbpwd +    
  146.              userName + password);
  147.              return "loginfailure" ;
  148.         }
  149.     }
  150.  }
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. }
  158.        
  159. String sql = "Select * from users where username = ?";
  160.        
  161. Statement statement = conn.createStatement(sql);
  162. statement.setString(1, username);