Skip to content

Instantly share code, notes, and snippets.

@mfrw
Created September 29, 2016 20:01
Show Gist options
  • Save mfrw/4abf22d07aad53f12823b1a4a8fae64c to your computer and use it in GitHub Desktop.
Save mfrw/4abf22d07aad53f12823b1a4a8fae64c to your computer and use it in GitHub Desktop.
package controller;
import java.sql.*;
import model.HealthAppModel;
public class HealthAppController {
HealthAppModel database;
public HealthAppController() {
this.database = new HealthAppModel();
}
/* if present in database then return true */
boolean checkEmail(String email) {
try {
ResultSet r = database.dbQueryEmail(email);
if(r.next()) {
if(r.getString(1).equals(email))
return true;
}
}
catch(SQLException e) {
System.out.println(e);
return false;
}
return false;
}
/* if present in db the return true */
boolean checkUsername(String username) {
try {
ResultSet r = database.dbQueryUsername(username);
if(r.next()) {
if(r.getString(1).equals(username))
return true;
}
}
catch(SQLException e) {
System.out.println(e);
return false;
}
return false;
}
public User login(String email, String password) {
try {
ResultSet r = database.dbLogin(email, password);
if(r.next()) {
if(r.getInt(16) <= 3) { /* means EndUser */
ResultSet extrainfo = database.dbGetEndUser(r.getString(1));
if(extrainfo.next()) {
return new EndUser(/*fill the constructor */);
}
}
else if(r.getInt(16) == 4) { /* means Admin */
ResultSet extrainfo = database.dbGetAdmin(r.getString(1));
if(extrainfo.next()) {
return new Administrator(/* fill the constructor */);
}
}
else {
ResultSet extrainfo = database.dbGetMod(r.getString(1));
if(extrainfo.next()) {
return new Moderator(/* fill the const */);
}
}
}
else
return null;
}
catch(SQLException e){
System.out.println(e);
}
return null;
}
void register(User u) {
database.dbRegisterUser(u);
}
}
package model;
import java.sql.*;
public class HealthAppModel {
private static String JDBC = "com.mysql.jdbc.Driver";
private static String DB = "jdbc:mysql://127.0.0.1:3306/smarthealthdb?autoReconnect=true&useSSl=false";
private static String USER = "mfrw";
private static String PASS = "toor";
public static Connection DBcon = null;
public HealthAppModel() {
try {
Class.forName(JDBC);
DBcon = DriverManager.getConnection(DB, USER, PASS);
}
catch(SQLException e) {
System.out.println(e);
DBcon = null;
}
catch(ClassNotFoundException e){
System.out.println(e);
}
}
/* This function Inserts into 2 tables, one is the User table and the other
* is the table depending on the user type
* */
void dbRegisterUser(User u) {
try {
PreparedStatement ps = HealthAppModel.DBcon.prepareStatement("INSERT INTO User VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1, u.getUsername());
ps.setString(2, u.getPassword());
ps.setString(3, u.getPrimaryemail());
ps.setString(4, u.getSecondaryemail());
ps.setString(5, u.getFirstname());
ps.setString(6, u.getLastname());
ps.setString(7, u.getAboutme());
ps.setString(8, u.getLink1());
ps.setString(9, u.getLink2());
ps.setString(10, u.getLink3());
ps.setString(11, u.getStreetnumber());
ps.setString(12, u.getStreetname());
ps.setString(13, u.getMajormunicipality());
ps.setString(14, u.getGoverningdistrict());
ps.setString(15, u.getPostalcode());
ps.setInt(16, u.getUsertype());
ps.setInt(17, u.getStatus());
ps.executeUpdate();
}
catch(SQLException e) {
System.out.println(e);
}
if(u.getUsertype()<=3){
try {
PreparedStatement ps = HealthAppModel.DBcon.prepareStatement("INSERT INTO EndUser VALUES (?, 1, CURDATE())");
ps.setString(1, u.getUsername());
ps.executeUpdate();
}
catch(SQLException e) {
System.out.print(e);
}
}
else if(u.getUsertype()==4){
try {
PreparedStatement ps = HealthAppModel.DBcon.prepareStatement("INSERT INTO Administrator VALUES (?, ?)");
ps.setString(1, u.getUsername());
ps.setInt(2, u.getUsertype());
ps.executeUpdate();
}
catch (SQLException e) {
System.out.println(e);
}
}
else{
try {
Moderator m = (Moderator) u;
PreparedStatement ps = HealthAppModel.DBcon.prepareStatement("INSERT INTO Moderator VALUES (?, ?)");
ps.setString(1, u.getUsername());
ps.setInt(2, u.getEmergencycontact());
ps.executeUpdate();
}
catch (SQLException e) {
System.out.println(e);
}
}
}
/* Get the details from the User table corresponding to the email and pass */
public ResultSet dbLogin(String email, String pass) {
try {
PreparedStatement ps = DBcon.prepareStatement("Select * from User where Email1 = ? and Password = ?");
ps.setString(1,email);
ps.setString(2, pass);
ResultSet r = ps.executeQuery();
return r;
}
catch(SQLException e){
System.out.println(e);
return null;
}
}
/* Get EndUser Details from EndUser table */
public ResultSet dbGetEndUser(String uname) {
try {
PreparedStatement ps = DBcon.prepareStatement("Select * from EndUser where Username = ?");
ps.setString(1, uname);
ResultSet r = ps.executeQuery();
return r;
}
catch(SQLException e){
System.out.println(e);
return null;
}
}
/* Get Administrator Details form the Admin table */
public ResultSet dbGetAdmin(String uname) {
try {
PreparedStatement ps = DBcon.prepareStatement("Select * from Administrator where Username = ?");
ps.setString(1, uname);
ResultSet r = ps.executeQuery();
return r;
}
catch(SQLException e){
System.out.println(e);
return null;
}
}
/* Get Mod details from Mod table */
public ResultSet dbGetMod(String uname) {
try {
PreparedStatement ps = DBcon.prepareStatement("Select * from Moderator where Username = ?");
ps.setString(1, uname);
ResultSet r = ps.executeQuery();
return r;
}
catch(SQLException e){
System.out.println(e);
return null;
}
}
/* check unique uname */
public ResultSet dbQueryUsername(String username) {
try {
PreparedStatement ps = DBcon.prepareStatement("SELECT Username FROM User WHERE Username=?");
ps.setString(1, username);
ResultSet r = ps.executeQuery();
return r;
}
catch(SQLException e){
System.out.println(e);
return null;
}
}
/* check unique email */
public ResultSet dbQueryEmail(String email) {
try {
PreparedStatement ps = DBcon.prepareStatement("SELECT Username FROM User WHERE Username=?");
ps.setString(1, email);
ResultSet r = ps.executeQuery();
return r;
}
catch(SQLException e){
System.out.println(e);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment