Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FormValidationBean.java
- import java.io.IOException;
- import java.sql.SQLException;
- import javax.inject.Named;
- import javax.enterprise.context.Dependent;
- import javax.faces.component.UIComponent;
- import javax.faces.validator.ValidatorException;
- import javax.faces.application.FacesMessage;
- import javax.faces.component.UIInput;
- import javax.faces.context.FacesContext;
- /**
- *
- * @author Jon
- */
- @Named(value = "formValidationBean")
- @Dependent
- public class FormValidationBean {
- private String forename;
- private String surname;
- private String email;
- private String password;
- private int luckyNumber;
- /**
- * Creates a new instance of FormValidationBean
- */
- public FormValidationBean() {
- }
- public String getForename() {
- return forename;
- }
- public void setForename(String aForename) {
- forename = aForename;
- }
- public String getSurname() {
- return surname;
- }
- public void setSurname(String aSurname) {
- surname = aSurname;
- }
- public String getName() {
- return forename + " " + surname;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String anEmail) {
- email = anEmail;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String aPassword) {
- password = aPassword;
- }
- public void setLuckyNumber(int aLuckyNumber) {
- luckyNumber = aLuckyNumber;
- }
- public int getLuckyNumber() {
- return luckyNumber;
- }
- public String register() throws IOException, ClassNotFoundException, SQLException{
- DatabaseManager dbm = new DatabaseManager();
- if (dbm.add(forename, surname, password, luckyNumber, email)){
- return "welcomeUser";
- } else {
- return "error";
- }
- }
- public String login() throws IOException, ClassNotFoundException, SQLException{
- DatabaseManager dbm = new DatabaseManager();
- if (dbm.verifyLogin(email,password)){
- return "welcomeUser";
- } else {
- return "error";
- }
- }
- }
- DatabaseManager.java
- import java.sql.*;
- import java.io.*;
- /**
- *
- * @author Defa1t
- */
- public class DatabaseManager {
- private static InputStream stream;
- private static Connection conn;
- private static Statement st;
- private String tableName = "user";
- /**
- * Constructor
- *
- * @throws IOException Handles IOExceptions
- * @throws ClassNotFoundException Handles CNFE
- * @throws SQLException Handles SQL Exceptions
- */
- public DatabaseManager() throws IOException, ClassNotFoundException, SQLException {
- stream = DatabaseManager.class.getResourceAsStream("/database.properties"); // create a stream from the db.properties
- SimpleDataSource.init(stream); // initilize the datasource connection with stream info
- conn = SimpleDataSource.getConnection(); // get connection from the data source
- st = conn.createStatement(); // create a statement from the connection
- }
- public boolean verifyLogin(String email, String password) throws SQLException {
- return st.execute("SELECT * FROM " + tableName + " WHERE email='" + email + "' AND password='" + password + "';");
- }
- public boolean add(String forename, String surname, String password, int registeredLuckyNumber, String email) throws SQLException {
- return st.execute("INSERT INTO user(foreName,surName,password,registeredLuckyNumber,email) VALUES('" + forename + "','" + surname + "','" + password + "'," + registeredLuckyNumber + ",'"+email+"');");
- }
- }
- SimpleDataSource.java
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Properties;
- import java.io.*;
- /**
- * A simple data source for getting database connections.
- */
- public class SimpleDataSource {
- /**
- * Initializes the data source.
- *
- * @param fileName the name of the property file that contains the database
- * driver, url, username and password
- */
- public static void init(InputStream fileName)
- throws IOException, ClassNotFoundException {
- Properties props = new Properties();
- props.load(fileName);
- String driver = props.getProperty("jdbc.driver");
- url = props.getProperty("jdbc.url");
- String username = props.getProperty("jdbc.username");
- String password = props.getProperty("jdbc.password");
- Class.forName(driver);
- }
- /**
- * Gets a connection to the database.
- *
- * @return the database connection
- */
- public static Connection getConnection() throws SQLException {
- return DriverManager.getConnection(url,
- username, password);
- }
- private static Connection conn;
- private static String url;
- private static String username;
- private static String password;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement