Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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.
- */
- package duktiakos_project1;
- import static duktiakos_project1.Authent.DB_URL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Vector;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.GET;
- import javax.ws.rs.POST;
- import javax.ws.rs.PUT;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.QueryParam;
- import javax.ws.rs.core.Context;
- import javax.ws.rs.core.UriInfo;
- import org.json.simple.JSONObject;
- /**
- * REST Web Service
- *
- * @author χατζογλου
- */
- @Path("insertstud")
- public class Insertstud {
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost:3306/mydb";
- static final String USER = "root";
- static final String PASS = "";
- @Context
- private UriInfo context;
- /**
- * Creates a new instance of InsertstudResource
- */
- public Insertstud() {
- }
- /**
- * Retrieves representation of an instance of duktiakos_project1.InsertstudResource
- * @return an instance of java.lang.String
- */
- @POST
- @Produces("application/json")
- @Consumes("text/plain")
- public String insgrd(@QueryParam ("AM") String AM,@QueryParam ("Name")String name,@QueryParam ("Surname")String surname,@QueryParam ("Semester")String semester) throws ClassNotFoundException, SQLException {
- Connection conn = null;
- Statement stmt = null;
- //Δήλωση JDBC driver
- Class.forName("com.mysql.jdbc.Driver");
- //Άνοιγμα σύνδεσης
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- //Εκτέλεση ερωτήματος
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- sql ="insert into mathitis(Name,Surname,Semester,AM) values ('"+name+"','"+surname+"','"+semester+"','"+AM+"')";
- stmt.executeUpdate(sql);
- JSONObject obj = new JSONObject();
- obj.put("Name", name);
- obj.put("Surname", surname);
- obj.put("Semester", semester);
- obj.put("AM", AM);
- stmt.close();
- conn.close();
- return obj.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement