RetrieveMusicFiles.java Source code

Java tutorial

Introduction

Here is the source code for RetrieveMusicFiles.java

Source

/*
 * 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.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Sweta
 */
@WebServlet(urlPatterns = { "/RetrieveMusicFiles" })
public class RetrieveMusicFiles extends HttpServlet {

    private String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private String DB_URL = "jdbc:mysql://localhost:3306/AndroidFamilyMusicBrowserDB";
    // Database credentials
    private String USER = "root";
    private String PASS = "";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String android_id = request.getParameter("androidId");

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {

            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

            String sql = "select a.album_title, a.album_artist, a.album_composer, a.release_year, "
                    + "s.song_title, s.genre, s.artist_name from album a, song s, user_music_data u "
                    + "where u.android_id='" + android_id + "' and u.song_id=s.song_id and s.album_id=a.album_id";
            rs = stmt.executeQuery(sql);
            JSONObject json = new JSONObject();
            JSONArray jArray = new JSONArray();
            while (rs.next()) {
                JSONObject element = new JSONObject();
                element.put("title", rs.getString("s.song_title"));
                element.put("album", rs.getString("a.album_title"));
                element.put("artist", rs.getString("s.artist_name"));
                element.put("genre", rs.getString("s.genre"));
                element.put("album_artist", rs.getString("a.album_artist"));
                element.put("album_composer", rs.getString("a.album_composer"));
                element.put("album_year", rs.getString("a.release_year"));

                jArray.put(element);
            }
            json.put("data", jArray);
            out.print(json);
            //out.print(resultJSON);
        } catch (Exception se) {
            //out.println("Exception preparing or processing query: " + se);
            se.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {

            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}