Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. FormValidationBean.java
  2.  
  3. import java.io.IOException;
  4. import java.sql.SQLException;
  5. import javax.inject.Named;
  6. import javax.enterprise.context.Dependent;
  7. import javax.faces.component.UIComponent;
  8. import javax.faces.validator.ValidatorException;
  9. import javax.faces.application.FacesMessage;
  10. import javax.faces.component.UIInput;
  11. import javax.faces.context.FacesContext;
  12.  
  13. /**
  14. *
  15. * @author Jon
  16. */
  17. @Named(value = "formValidationBean")
  18. @Dependent
  19. public class FormValidationBean {
  20.  
  21. private String forename;
  22. private String surname;
  23. private String email;
  24. private String password;
  25. private int luckyNumber;
  26.  
  27. /**
  28. * Creates a new instance of FormValidationBean
  29. */
  30. public FormValidationBean() {
  31. }
  32.  
  33. public String getForename() {
  34. return forename;
  35. }
  36.  
  37. public void setForename(String aForename) {
  38. forename = aForename;
  39. }
  40.  
  41. public String getSurname() {
  42. return surname;
  43. }
  44.  
  45. public void setSurname(String aSurname) {
  46. surname = aSurname;
  47. }
  48.  
  49. public String getName() {
  50. return forename + " " + surname;
  51. }
  52.  
  53. public String getEmail() {
  54. return email;
  55. }
  56.  
  57. public void setEmail(String anEmail) {
  58. email = anEmail;
  59. }
  60.  
  61. public String getPassword() {
  62. return password;
  63. }
  64.  
  65. public void setPassword(String aPassword) {
  66. password = aPassword;
  67. }
  68.  
  69. public void setLuckyNumber(int aLuckyNumber) {
  70. luckyNumber = aLuckyNumber;
  71. }
  72.  
  73. public int getLuckyNumber() {
  74. return luckyNumber;
  75. }
  76.  
  77. public String register() throws IOException, ClassNotFoundException, SQLException{
  78. DatabaseManager dbm = new DatabaseManager();
  79. if (dbm.add(forename, surname, password, luckyNumber, email)){
  80. return "welcomeUser";
  81. } else {
  82. return "error";
  83. }
  84. }
  85.  
  86. public String login() throws IOException, ClassNotFoundException, SQLException{
  87. DatabaseManager dbm = new DatabaseManager();
  88. if (dbm.verifyLogin(email,password)){
  89. return "welcomeUser";
  90. } else {
  91. return "error";
  92. }
  93. }
  94.  
  95. }
  96.  
  97. DatabaseManager.java
  98.  
  99.  
  100. import java.sql.*;
  101. import java.io.*;
  102.  
  103. /**
  104. *
  105. * @author Defa1t
  106. */
  107. public class DatabaseManager {
  108.  
  109. private static InputStream stream;
  110. private static Connection conn;
  111. private static Statement st;
  112. private String tableName = "user";
  113.  
  114. /**
  115. * Constructor
  116. *
  117. * @throws IOException Handles IOExceptions
  118. * @throws ClassNotFoundException Handles CNFE
  119. * @throws SQLException Handles SQL Exceptions
  120. */
  121. public DatabaseManager() throws IOException, ClassNotFoundException, SQLException {
  122. stream = DatabaseManager.class.getResourceAsStream("/database.properties"); // create a stream from the db.properties
  123. SimpleDataSource.init(stream); // initilize the datasource connection with stream info
  124. conn = SimpleDataSource.getConnection(); // get connection from the data source
  125. st = conn.createStatement(); // create a statement from the connection
  126. }
  127.  
  128. public boolean verifyLogin(String email, String password) throws SQLException {
  129. return st.execute("SELECT * FROM " + tableName + " WHERE email='" + email + "' AND password='" + password + "';");
  130. }
  131.  
  132. public boolean add(String forename, String surname, String password, int registeredLuckyNumber, String email) throws SQLException {
  133. return st.execute("INSERT INTO user(foreName,surName,password,registeredLuckyNumber,email) VALUES('" + forename + "','" + surname + "','" + password + "'," + registeredLuckyNumber + ",'"+email+"');");
  134. }
  135. }
  136.  
  137.  
  138. SimpleDataSource.java
  139.  
  140. import java.sql.Connection;
  141. import java.sql.DriverManager;
  142. import java.sql.SQLException;
  143. import java.io.FileInputStream;
  144. import java.io.IOException;
  145. import java.util.Properties;
  146. import java.io.*;
  147.  
  148. /**
  149. * A simple data source for getting database connections.
  150. */
  151. public class SimpleDataSource {
  152.  
  153. /**
  154. * Initializes the data source.
  155. *
  156. * @param fileName the name of the property file that contains the database
  157. * driver, url, username and password
  158. */
  159. public static void init(InputStream fileName)
  160. throws IOException, ClassNotFoundException {
  161. Properties props = new Properties();
  162. props.load(fileName);
  163.  
  164. String driver = props.getProperty("jdbc.driver");
  165. url = props.getProperty("jdbc.url");
  166. String username = props.getProperty("jdbc.username");
  167. String password = props.getProperty("jdbc.password");
  168.  
  169. Class.forName(driver);
  170. }
  171.  
  172. /**
  173. * Gets a connection to the database.
  174. *
  175. * @return the database connection
  176. */
  177. public static Connection getConnection() throws SQLException {
  178. return DriverManager.getConnection(url,
  179. username, password);
  180. }
  181.  
  182. private static Connection conn;
  183. private static String url;
  184. private static String username;
  185. private static String password;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement