コード例 #1
0
  public Utente doRetrieveByIdPwd(String id, String pwd) throws PersistenceException {
    Utente utente = null;
    DataSource ds = new DataSource();
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet result = null;

    try {
      connection = ds.getConnection();
      String retrieve = "SELECT utente, pwd,  isadmin,id FROM utenze WHERE utente=? and pwd=?";
      statement = connection.prepareStatement(retrieve);
      statement.setString(1, id);
      statement.setString(2, pwd);
      result = statement.executeQuery();
      if (result.next()) {
        utente = new Utente();
        utente.setUser(id);
        utente.setPwd(result.getString("pwd"));
        utente.setIsAdmin(result.getBoolean("isadmin"));
        utente.setId(result.getInt("id"));
        utente.setNome(new ClienteDAOImpl().getClienteById(utente.getId()).getNome());
      }
    } catch (SQLException e) {
      throw new PersistenceException(e.getMessage());
    } finally {
      try {
        if (result != null) result.close();
        if (statement != null) statement.close();
        if (connection != null) connection.close();
      } catch (SQLException e) {
        throw new PersistenceException(e.getMessage());
      }
    }
    return utente;
  }
コード例 #2
0
 private void doInsert(Utente utente) throws PersistenceException {
   DataSource ds = new DataSource();
   Connection connection = ds.getConnection();
   PreparedStatement statement = null;
   try {
     String update = "insert into utenze (isadmin,pwd,utente,id) values (?,?,?,?)";
     statement = connection.prepareStatement(update);
     statement.setBoolean(1, utente.getIsAdmin());
     statement.setString(2, utente.getPwd());
     statement.setString(3, utente.getUser());
     statement.setInt(4, utente.getId());
     statement.executeUpdate();
   } catch (SQLException e) {
     throw new PersistenceException(e.getMessage());
   } finally {
     try {
       if (statement != null) statement.close();
       if (connection != null) connection.close();
     } catch (SQLException e) {
       throw new PersistenceException(e.getMessage());
     }
   }
 }