Advertisement
Guest User

Untitled

a guest
Feb 18th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 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.SQLException;
  13. import java.sql.Statement;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Vector;
  17. import javax.ws.rs.Consumes;
  18. import javax.ws.rs.GET;
  19. import javax.ws.rs.POST;
  20. import javax.ws.rs.PUT;
  21. import javax.ws.rs.Path;
  22. import javax.ws.rs.Produces;
  23. import javax.ws.rs.QueryParam;
  24. import javax.ws.rs.core.Context;
  25. import javax.ws.rs.core.UriInfo;
  26. import org.json.simple.JSONObject;
  27.  
  28. /**
  29. * REST Web Service
  30. *
  31. * @author χατζογλου
  32. */
  33. @Path("insertstud")
  34. public class Insertstud {
  35. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  36. static final String DB_URL = "jdbc:mysql://localhost:3306/mydb";
  37. static final String USER = "root";
  38. static final String PASS = "";
  39. @Context
  40. private UriInfo context;
  41.  
  42. /**
  43. * Creates a new instance of InsertstudResource
  44. */
  45. public Insertstud() {
  46. }
  47.  
  48. /**
  49. * Retrieves representation of an instance of duktiakos_project1.InsertstudResource
  50. * @return an instance of java.lang.String
  51. */
  52. @POST
  53. @Produces("application/json")
  54. @Consumes("text/plain")
  55.  
  56. public String insgrd(@QueryParam ("AM") String AM,@QueryParam ("Name")String name,@QueryParam ("Surname")String surname,@QueryParam ("Semester")String semester) throws ClassNotFoundException, SQLException {
  57. Connection conn = null;
  58. Statement stmt = null;
  59. //Δήλωση JDBC driver
  60. Class.forName("com.mysql.jdbc.Driver");
  61. //Άνοιγμα σύνδεσης
  62. System.out.println("Connecting to database...");
  63. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  64. //Εκτέλεση ερωτήματος
  65. System.out.println("Creating statement...");
  66. stmt = conn.createStatement();
  67. String sql;
  68. sql ="insert into mathitis(Name,Surname,Semester,AM) values ('"+name+"','"+surname+"','"+semester+"','"+AM+"')";
  69. stmt.executeUpdate(sql);
  70. JSONObject obj = new JSONObject();
  71. obj.put("Name", name);
  72. obj.put("Surname", surname);
  73. obj.put("Semester", semester);
  74. obj.put("AM", AM);
  75. stmt.close();
  76. conn.close();
  77. return obj.toString();
  78.  
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement