Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.api.dao.user; import com.api.domain.User; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; /** * * @author mark */ @Component(value = "sqlUserDao") public class SqlUserDao implements UserDao { private final DataSource dataSource; private final Environment environment; private final static String FIND_ALL = "find.all"; private final static String FIND_BY_ID = "find.by.id"; @Autowired public SqlUserDao(DataSource dataSource, Environment environment) { this.dataSource = dataSource; this.environment = environment; } @Override public Collection<User> findAll() throws SQLException { User user; List<User> users = new ArrayList<>(); Connection connection = dataSource.getConnection(); String findAllQuery = environment.getRequiredProperty(FIND_ALL); try (Statement stmt = connection.createStatement()) { ResultSet rs = stmt.executeQuery(findAllQuery); while (rs.next()) { user = new User(); String firstName = rs.getString("firstName"); String lastName = rs.getString("lastName"); String email = rs.getString("email"); int dbid = rs.getInt("id"); user.setFirstName(firstName); user.setLastName(lastName); user.setId(dbid); user.setEmail(email); users.add(user); } } catch (SQLException sqle) { sqle.printStackTrace(); throw sqle; } return users; } @Override public User findById(String id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public User findByEmail(String email) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public Collection<User> findByFirstNameAndLastName(String firstName, String lastName) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public User updateUserWithId(String id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public User updateUserWithEmail(String id) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public Collection<User> updateUsersWithFirstAndLastName(String firstName, String LastName) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public Collection<User> updateUserWithFirstName(String firstName) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public Collection<User> updateUserWithLastName(String lastName) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }