Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
4.8 kB
2
Indexable
Never
// vazni importi 
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

//import model.Automobil;
//import model.dto.AutomobiliVozaciDTO;
import models.User;

import java.util.ArrayList;

public class DAO {
      private DataSource ds;

    private static String SELECT_USER = "SELECT * FROM users WHERE username = ? AND password = ?";
    private static String INSERT_USER = "INSERT INTO `users` (`username`, `password`, `email`) VALUES (?, ?, ?)";
    private static String SELECT_USER_NAME = "SELECT * FROM users WHERE username = ?";
    private static String SELECT_USER_EMAIL = "SELECT * FROM users WHERE email = ?";
	
	// DEFINICIJA KONSTRUKTORA ZA PODESAVNJE KONEKCIJE � UVEK ISTO
	public DAO(){
	try {
		InitialContext cxt = new InitialContext();
		if ( cxt == null ) { 
		} 
		ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/mysql" ); 
		if ( ds == null ) { 
		} 		
		} catch (NamingException e) {
		}
	}
	
	public void insertUser(User pom){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		// POMOCNE PROMENLJIVE ZA KONKRETNU METODU 
				
            try {
			con = ds.getConnection();
			pstm = con.prepareStatement(INSERT_USER);

			// DOPUNJAVANJE SQL STRINGA, SVAKI ? SE MORA PODESITI 
			pstm.setString(1, pom.getUsername());
			pstm.setString(2, pom.getPassword());
			pstm.setString(3, pom.getEmail());
			pstm.execute();

//****POCETAK AKO UPIT VRACA REZULTAT TREBA SLEDECI DEO 
			
//****KRAJ OBRADE ResultSet-a	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// VRACANJE REZULTATA AKO METODA VRACA REZULTAT
	} 
	
	public User checkUsername(String username) {
	    Connection con = null;
	    PreparedStatement pstm = null;
	    ResultSet rs = null;
	    User user = null;

	    try {
	        con = ds.getConnection(); 
	        pstm = con.prepareStatement(SELECT_USER_NAME);
	        pstm.setString(1, username);
	        rs = pstm.executeQuery();

	        if (rs.next()) {
	            user = new User();
	            user.setUsername(rs.getString("username"));
	        }
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            if (rs != null) {
	                rs.close();
	            }
	            if (pstm != null) {
	                pstm.close();
	            }
	            if (con != null) {
	                con.close();
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	    }

	    return user;
	}
	
	public User checkEmail(String email) {
		Connection con = null;
	    PreparedStatement pstm = null;
	    ResultSet rs = null;
	    User user = null;

	    try {
	        con = ds.getConnection(); 
	        pstm = con.prepareStatement(SELECT_USER_EMAIL);
	        pstm.setString(1, email);
	        rs = pstm.executeQuery();

	        if (rs.next()) {
	            user = new User();
	            user.setEmail(email);
	        }
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            if (rs != null) {
	                rs.close();
	            }
	            if (pstm != null) {
	                pstm.close();
	            }
	            if (con != null) {
	                con.close();
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	    }

	    return user;
	}

	
	public User selectUser(String username, String password) {
	    Connection con = null;
	    PreparedStatement pstm = null;
	    ResultSet rs = null;
	    User user = null;

	    try {
	        con = ds.getConnection();
	        pstm = con.prepareStatement(SELECT_USER);
	        pstm.setString(1, username);
	        pstm.setString(2, password);
	        rs = pstm.executeQuery();

	        if (rs.next()) {
	            user = new User();
	            user.setUsername(rs.getString("username"));
	            user.setPassword(rs.getString("password"));
	        }
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            if (rs != null) {
	                rs.close();
	            }
	            if (pstm != null) {
	                pstm.close();
	            }
	            if (con != null) {
	                con.close();
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	    }

	    return user;
	}
Leave a Comment