Advertisement
Guest User

Untitled

a guest
Apr 16th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. public class HolaMundoFinalServlet extends HttpServlet {
  2. /**
  3. *
  4. */
  5. private static final long serialVersionUID = -6310899419273229136L;
  6. ResultSet rs=null;
  7. Connection c = null;
  8. PreparedStatement ps=null;
  9.  
  10. private Connection conectar(){
  11. Connection con = null;
  12. try{
  13. DriverManager.registerDriver(new AppEngineDriver());
  14. con = (Connection) DriverManager
  15. .getConnection("jdbc:google:rdbms://diplomadoandroid:city/GifterApp");
  16. } catch (SQLException e) {
  17. e.printStackTrace();
  18. }
  19. return con;
  20. }
  21.  
  22. private String getJSON(){
  23. JSONObject ob = new JSONObject();
  24. JSONObject tmp=new JSONObject();
  25. JSONArray jsa= new JSONArray();
  26. try {
  27. ps = c.prepareStatement("SELECT * FROM regalos");
  28. //ps.setString(1, "");
  29. rs = ps.executeQuery();
  30. while(rs.next()){
  31. tmp=new JSONObject();
  32. tmp.put("nombre", rs.getString(2));
  33. tmp.put("tipo", rs.getString(3));
  34. tmp.put("descripcion", rs.getString(4));
  35. tmp.put("precio", rs.getString(5));
  36. tmp.put("tienda", rs.getString(6));
  37. tmp.put("usuario", rs.getString(7));
  38. jsa.add(tmp);
  39. }
  40. ps.close();
  41. rs.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45.  
  46. ob.put("Regalos", jsa);
  47. return ob.toString();
  48. }
  49.  
  50. private String getJSON(String id){
  51. JSONObject ob = new JSONObject();
  52. JSONObject tmp=new JSONObject();
  53. JSONArray jsa= new JSONArray();
  54. try {
  55. ps = c.prepareStatement("SELECT * FROM regalos WHERE id_regalo = ?");
  56. ps.setString(1, id);
  57. rs = ps.executeQuery();
  58. while(rs.next()){
  59. tmp=new JSONObject();
  60. tmp.put("nombre", rs.getString(2));
  61. tmp.put("tipo", rs.getString(3));
  62. tmp.put("descripcion", rs.getString(4));
  63. tmp.put("precio", rs.getString(5));
  64. tmp.put("tienda", rs.getString(6));
  65. tmp.put("usuario", rs.getString(7));
  66. jsa.add(tmp);
  67. }
  68. ps.close();
  69. rs.close();
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73.  
  74. ob.put("Regalos", jsa);
  75. return ob.toString();
  76. }
  77.  
  78. public void doGet(HttpServletRequest req, HttpServletResponse resp)
  79. throws ServletException, IOException {
  80.  
  81. resp.setHeader("Access-Control-Allow-Origin", "*");
  82.  
  83. resp.setContentType("text/javascript");
  84. String monumento = req.getParameter("regalo");
  85. c = conectar();
  86.  
  87. final PrintWriter out = resp.getWriter();
  88. String respuesta = null;
  89. if(monumento!=null){
  90. respuesta = getJSON(monumento);
  91. }else{
  92. respuesta = getJSON();
  93. }
  94.  
  95. out.write(respuesta);
  96. out.flush();
  97. out.close();
  98.  
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement