src/main/java/de/uapcore/lightpit/dao/UserDao.java

changeset 34
824d4042c857
parent 33
fd8c40ff78c3
child 62
833e0385572a
equal deleted inserted replaced
33:fd8c40ff78c3 34:824d4042c857
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2018 Mike Becker. All rights reserved. 4 * Copyright 2018 Mike Becker. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
28 */ 28 */
29 package de.uapcore.lightpit.dao; 29 package de.uapcore.lightpit.dao;
30 30
31 import de.uapcore.lightpit.entities.User; 31 import de.uapcore.lightpit.entities.User;
32 32
33 import java.sql.Connection; 33 public interface UserDao extends GenericDao<User> {
34 import java.sql.ResultSet;
35 import java.sql.SQLException;
36 import java.sql.Statement;
37 import java.util.ArrayList;
38 import java.util.List;
39 34
40 public class UserDao {
41
42 /**
43 * Maps SQL columns to POJO fields.
44 *
45 * @param result the database result set
46 * @param user the POJO
47 * @throws SQLException on any kind of SQL errors
48 */
49 protected void mapColumns(ResultSet result, User user) throws SQLException {
50 user.setUserID(result.getInt("userid"));
51 user.setUsername(result.getString("username"));
52 user.setGivenname(result.getString("givenname"));
53 user.setLastname(result.getString("lastname"));
54 }
55
56 /**
57 * Returns a list of all users ordered by their username.
58 * <p>
59 * Does not return reserved system users with negative user IDs.
60 *
61 * @param conn the connection to use
62 * @return a list of all users
63 * @throws SQLException on any kind of SQL errors
64 */
65 public List<User> listAll(Connection conn) throws SQLException {
66 List<User> list = new ArrayList<>();
67 try (
68 Statement stmt = conn.createStatement();
69 ResultSet result = stmt.executeQuery(
70 "SELECT * FROM lpitcore_user WHERE userid >= 0 ORDER BY username")) {
71 while (result.next()) {
72 final User user = new User();
73 mapColumns(result, user);
74 list.add(user);
75 }
76 }
77 return list;
78 }
79 } 35 }

mercurial