Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.socialka.model.user;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import com.socialka.db.DBConnection;
- public class UserDAO implements IUserDAO{
- private Connection connection;
- public UserDAO() throws Exception {
- this.connection = DBConnection.getInstance().getConnection();
- }
- @Override
- public int login(User user) throws UserException {
- try {
- Statement statement = this.connection.createStatement();
- ResultSet resultSet = statement.executeQuery(
- String.format("select count(*) from users where username = %s and password = %s);",
- user.getUsername(), user.getPassword()));
- if (resultSet.next()) {
- return resultSet.getInt("id");
- }
- throw new UserException("Invalid user name or password");
- } catch (SQLException e) {
- e.printStackTrace();
- throw new UserException("DB is not working", e);
- }
- }
- @Override
- public void register() {
- }
- public static void main(String[] args) throws Exception {
- UserDAO dao = new UserDAO();
- int id = dao.login(new User("Guluba", "3333"));
- System.out.println(id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement