Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HolaMundoFinalServlet extends HttpServlet {
- /**
- *
- */
- private static final long serialVersionUID = -6310899419273229136L;
- ResultSet rs=null;
- Connection c = null;
- PreparedStatement ps=null;
- private Connection conectar(){
- Connection con = null;
- try{
- DriverManager.registerDriver(new AppEngineDriver());
- con = (Connection) DriverManager
- .getConnection("jdbc:google:rdbms://diplomadoandroid:city/GifterApp");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return con;
- }
- private String getJSON(){
- JSONObject ob = new JSONObject();
- JSONObject tmp=new JSONObject();
- JSONArray jsa= new JSONArray();
- try {
- ps = c.prepareStatement("SELECT * FROM regalos");
- //ps.setString(1, "");
- rs = ps.executeQuery();
- while(rs.next()){
- tmp=new JSONObject();
- tmp.put("nombre", rs.getString(2));
- tmp.put("tipo", rs.getString(3));
- tmp.put("descripcion", rs.getString(4));
- tmp.put("precio", rs.getString(5));
- tmp.put("tienda", rs.getString(6));
- tmp.put("usuario", rs.getString(7));
- jsa.add(tmp);
- }
- ps.close();
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- ob.put("Regalos", jsa);
- return ob.toString();
- }
- private String getJSON(String id){
- JSONObject ob = new JSONObject();
- JSONObject tmp=new JSONObject();
- JSONArray jsa= new JSONArray();
- try {
- ps = c.prepareStatement("SELECT * FROM regalos WHERE id_regalo = ?");
- ps.setString(1, id);
- rs = ps.executeQuery();
- while(rs.next()){
- tmp=new JSONObject();
- tmp.put("nombre", rs.getString(2));
- tmp.put("tipo", rs.getString(3));
- tmp.put("descripcion", rs.getString(4));
- tmp.put("precio", rs.getString(5));
- tmp.put("tienda", rs.getString(6));
- tmp.put("usuario", rs.getString(7));
- jsa.add(tmp);
- }
- ps.close();
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- ob.put("Regalos", jsa);
- return ob.toString();
- }
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- resp.setHeader("Access-Control-Allow-Origin", "*");
- resp.setContentType("text/javascript");
- String monumento = req.getParameter("regalo");
- c = conectar();
- final PrintWriter out = resp.getWriter();
- String respuesta = null;
- if(monumento!=null){
- respuesta = getJSON(monumento);
- }else{
- respuesta = getJSON();
- }
- out.write(respuesta);
- out.flush();
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement