Guest User

Untitled

a guest
Feb 20th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package duktiakos_project1;
  8.  
  9. import static duktiakos_project1.Authent.DB_URL;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Vector;
  18. import javax.ws.rs.Consumes;
  19. import javax.ws.rs.GET;
  20. import javax.ws.rs.POST;
  21. import javax.ws.rs.PUT;
  22. import javax.ws.rs.Path;
  23. import javax.ws.rs.Produces;
  24. import javax.ws.rs.QueryParam;
  25. import javax.ws.rs.core.Context;
  26. import javax.ws.rs.core.UriInfo;
  27. import org.json.simple.JSONObject;
  28.  
  29. /**
  30. * REST Web Service
  31. *
  32. * @author χατζογλου
  33. */
  34. @Path("insergrd")
  35. public class Updategrd {
  36. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  37. static final String DB_URL = "jdbc:mysql://localhost:3306/mydb";
  38. static final String USER = "root";
  39. static final String PASS = "";
  40. @Context
  41. private UriInfo context;
  42.  
  43. /**
  44. * Creates a new instance of InsertstudResource
  45. */
  46. public Updategrd() {
  47. }
  48.  
  49. /**
  50. * Retrieves representation of an instance of duktiakos_project1.InsertstudResource
  51. * @return an instance of java.lang.String
  52. */
  53. @GET
  54. @Produces("application/json")
  55. @Consumes("text/plain")
  56.  
  57. public String insgrd(@QueryParam ("username") String username,@QueryParam ("mathima")String mathima,@QueryParam ("grade") String vathmos) throws ClassNotFoundException, SQLException {
  58. Connection conn = null;
  59. Statement stmt = null;
  60. //Δήλωση JDBC driver
  61. Class.forName("com.mysql.jdbc.Driver");
  62. //Άνοιγμα σύνδεσης
  63. System.out.println("Connecting to database...");
  64. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  65. //Εκτέλεση ερωτήματος
  66. System.out.println("Creating statement...");
  67. stmt = conn.createStatement();
  68. String sql1,sql2,sql3;
  69. JSONObject obj = new JSONObject();
  70. sql1="select math_name,AM,mathimata.mathimata_id,mathitis.id_mathitis,grade from mathimata,grade,mathitis where AM='"+username+"' AND Math_name='"+mathima+"' AND mathimata.mathimata_id=grade.mathimata_id AND mathitis.id_mathitis=grade.id_mathitis ";
  71. ResultSet rs = stmt.executeQuery(sql1);
  72. while(rs.next()){
  73. String math_name=rs.getString("math_name");
  74. String AM=rs.getString("AM");
  75. String Mathima_id = rs.getString("mathimata_id");
  76. String Mathitis_id=rs.getString("id_mathitis");
  77. String grade = rs.getString("grade");
  78. if(!grade.equals(null))
  79. {
  80. sql2 ="update grade set grade ='"+vathmos+"' where mathimata_id='"+Mathima_id+"' AND id_mathitis='"+Mathitis_id+"'";
  81. stmt.executeUpdate(sql2);
  82. obj.put("Mathima",math_name );
  83. obj.put("AM",AM);
  84. obj.put("grade",vathmos);
  85.  
  86. }
  87. //else
  88. //{
  89. // System.out.println("bgike");
  90. // sql3 ="insert into grade values('"+vathmos+"','"+Mathima_id+"','"+Mathitis_id+"')";
  91. /// stmt.executeUpdate(sql3);
  92. //obj.put("Mathima",math_name );
  93. //obj.put("AM",AM);
  94. //obj.put("grade",vathmos);
  95. //}
  96.  
  97. rs.close();
  98. stmt.close();
  99. conn.close();
  100. return obj.toString();
  101.  
  102. }
  103.  
  104.  
  105. return null;
  106.  
  107. }
  108.  
  109. }
Add Comment
Please, Sign In to add comment