Exemplo n.º 1
0
 /** ajoute 1 au nombre de fois que la musique a �t� lu */
 public static synchronized void addLecture(Musique musique) {
   Connection connection = initConnection();
   try {
     Statement statement = connection.createStatement();
     statement.setQueryTimeout(30);
     statement.executeUpdate(
         "Update songs set nblecture = nblecture + 1 where title like '"
             + musique.getTitle()
             + "' and album like '"
             + musique.getAlbum()
             + "' and artist like '"
             + musique.getArtist()
             + "'");
     musique.getNbLecture().setNbLecture();
   } catch (SQLException e) {
     System.err.println(e.getMessage());
   } finally {
     try {
       if (connection != null) connection.close();
     } catch (SQLException e) {
       System.err.println(e);
     }
   }
 }
Exemplo n.º 2
0
  /**
   * @param recherche le mot a recherche dans la base de donnees
   * @param artistCheck si true, recherche dans artist
   * @param titleCheck si true, recherche dans title
   * @return la base de donnee dans un tableau d'objet
   */
  public static synchronized Object[][] recherche(
      String recherche, boolean artistCheck, boolean titleCheck) {
    Connection connection = initConnection();
    try {
      Statement statement = connection.createStatement();
      statement.setQueryTimeout(30);
      ResultSet rs;
      ArrayList<Object[]> l = new ArrayList<Object[]>();
      if (artistCheck && titleCheck)
        rs =
            statement.executeQuery(
                "select * from songs where artist like '%"
                    + recherche
                    + "%' or title like '%"
                    + recherche
                    + "%'");
      else if (artistCheck)
        rs = statement.executeQuery("select * from songs where artist like '%" + recherche + "%'");
      else if (titleCheck)
        rs = statement.executeQuery("select * from songs where title like '%" + recherche + "%'");
      else {
        if (recherche.length() == 0) rs = statement.executeQuery("select * from songs");
        else {
          String requete = "select * from songs where ";
          for (int i = 0; i < columnNames.length - 1; i++)
            requete += columnNames[i] + " like '%" + recherche + "%' or ";
          requete += "duration like '%" + recherche + "%'";
          rs = statement.executeQuery(requete);
        }
      }

      while (rs.next()) {
        ArrayList<StructureMusique> tmp = new ArrayList<StructureMusique>();
        Musique mus =
            new Musique(
                rs.getString("title"),
                rs.getString("album"),
                rs.getString("artist"),
                rs.getString("genre"),
                rs.getString("year"),
                rs.getString("duration"),
                Integer.parseInt(rs.getString("nblecture")));
        tmp.add(mus.getTitle());
        tmp.add(mus.getAlbum());
        tmp.add(mus.getArtist());
        tmp.add(mus.getGenre());
        // tmp.add(mus.getYear());
        // tmp.add(mus.getDuration());
        l.add(tmp.toArray());
      }
      Object retour[][] = new Object[l.size()][];
      for (int i = 0; i < l.size(); ++i) retour[i] = l.get(i);

      statement.close();
      connection.close();
      return retour;
    } catch (SQLException e) {
      System.err.println(e.getMessage());
    } finally {
      try {
        if (connection != null) connection.close();
      } catch (SQLException e) {
        System.err.println(e);
      }
    }
    return null;
  }