Blame view

src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java 2.82 KB
a800fde0   jcartign   First version, in...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  package fr.plil.sio.persistence.jdbc;
  
  import fr.plil.sio.persistence.api.User;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  import javax.sql.DataSource;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  @Repository
  public class UserRepositoryJdbc implements UserRepository {
  
      private static final Logger logger = LoggerFactory.getLogger(UserRepositoryJdbc.class);
  
      @Autowired
      private DataSource dataSource;
  
      @Override
      public User findByName(String name) {
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
              rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'");
              if (rs.next()) {
                  logger.debug("found user " + name);
                  User user = new User();
                  user.setId(rs.getLong("GROUP_ID"));
                  user.setName(rs.getString("NAME_C"));
                  return user;
              } else {
                  logger.debug("not found " + name);
                  return null;
              }
          } catch (SQLException e) {
              throw new UnsupportedOperationException("sql exception", e);
          } finally {
              try {
                  if (rs != null) {
                      rs.close();
                  }
                  if (stmt != null) {
                      stmt.close();
                  }
              } catch (SQLException e) {
                  throw new UnsupportedOperationException("sql exception during close", e);
  
              }
          }
      }
  
      @Override
      public void delete(Long id) {
          throw new IllegalStateException("not implemented !");
      }
  
      @Override
      public void save(User user) {
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
              stmt.executeUpdate("INSERT INTO USER_T (NAME_C) VALUES (\'" + user.getName() + "\')",
                      Statement.RETURN_GENERATED_KEYS);
              rs = stmt.getGeneratedKeys();
              if (rs.next()) {
                  user.setId(rs.getLong(1));
              } else {
                  throw new UnsupportedOperationException("default in key access");
              }
          } catch (SQLException e) {
              throw new UnsupportedOperationException("sql exception", e);
          } finally {
              try {
                  if (rs != null) {
                      rs.close();
                  }
                  if (stmt != null) {
                      stmt.close();
                  }
              } catch (SQLException e) {
                  throw new UnsupportedOperationException("sql exception during close", e);
              }
          }
  
      }
  }