Package

Source Code of DatabaseKoppeling

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.BASE64Encoder;

/**
* @author Mitch
*/
public class DatabaseKoppeling {
    // <editor-fold defaultstate="collapsed" desc="Properties">
    private Connection con;
    private String serverAdress = "localhost";
    private String portNumber   = "?";
    private String sid          = "?";
    private String url          = "jdbc:mysql://" + serverAdress + ":" + portNumber + "/" + sid;
    private String username     = "?"; // root";
    private String password     = "?";
    // </editor-fold>
   
    private void openConnection() throws SQLException {
        con = DriverManager.getConnection(url, username, password);
    }
   
    public boolean register(String inlogcode, String naam, String wachtwoord, boolean isFotograaf, String adres) {
        if(checkUsername(username)) {
            int isFoto;
            if(isFotograaf) {
                isFoto = 1;
            } else {
                isFoto = 0;
            }
            String query = "INSERT INTO Gebruiker (inlogcode, naam, wachtwoord, isFotograaf, adres) values('" + inlogcode + "', '" + naam + "', '" + encrypt(wachtwoord) + "', " + isFoto + ", '" + adres + "')";
            try {
                this.openConnection();
                con.createStatement().executeUpdate(query);  
                con.close();
                return true;
            } catch(SQLException ex) {
                Logger.getLogger(DatabaseKoppeling.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        } else {
            return false;
        }
    }
   
    private boolean checkUsername(String inlogcode) {
        String query = "SELECT inlogcode FROM Gebruiker WHERE inlogcode = '" + inlogcode + "' LIMIT 1";
        boolean succeeded = true;
        try {
            this.openConnection();
            succeeded = con.createStatement().executeQuery(query).next();
            con.close();
        } catch(SQLException ex) {
            Logger.getLogger(DatabaseKoppeling.class.getName()).log(Level.SEVERE, null, ex);
        }
        return succeeded;
    }
   
    public User login(String inlogcode, String wachtwoord) {
        String query = "SELECT (?) FROM Gebruiker WHERE username = '" + inlogcode + "' AND password = '" + encrypt(wachtwoord) + "'";
        User g = null;
        try {
            this.openConnection();
            ResultSet rs = con.createStatement().executeQuery(query);
            if(rs.next())
            {
                g = new User("?");
            }
            con.close();
        } catch(SQLException ex) {
            Logger.getLogger(DatabaseKoppeling.class.getName()).log(Level.SEVERE, null, ex);
        }
        return g;
    }
   
    private static String encrypt(String password) {
        try {
            BASE64Encoder enc = new BASE64Encoder();
            return byteArrayToHexString(MessageDigest.getInstance("MD5").digest(enc.encode(password.getBytes("UTF-8")).getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
            Logger.getLogger(DatabaseKoppeling.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
   
    private static String byteArrayToHexString(byte[] b) {
        String result = "";
        for (int i=0; i < b.length; i++) {
            result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
        }
        return result;
    }
}
TOP

Related Classes of DatabaseKoppeling

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.