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.ResultSet;
- 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("insergrd")
- public class Updategrd {
- 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 Updategrd() {
- }
- /**
- * Retrieves representation of an instance of duktiakos_project1.InsertstudResource
- * @return an instance of java.lang.String
- */
- @GET
- @Produces("application/json")
- @Consumes("text/plain")
- public String insgrd(@QueryParam ("username") String username,@QueryParam ("mathima")String mathima,@QueryParam ("grade") String vathmos) 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 sql1,sql2,sql3;
- JSONObject obj = new JSONObject();
- 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 ";
- ResultSet rs = stmt.executeQuery(sql1);
- while(rs.next()){
- String math_name=rs.getString("math_name");
- String AM=rs.getString("AM");
- String Mathima_id = rs.getString("mathimata_id");
- String Mathitis_id=rs.getString("id_mathitis");
- String grade = rs.getString("grade");
- if(!grade.equals(null))
- {
- sql2 ="update grade set grade ='"+vathmos+"' where mathimata_id='"+Mathima_id+"' AND id_mathitis='"+Mathitis_id+"'";
- stmt.executeUpdate(sql2);
- obj.put("Mathima",math_name );
- obj.put("AM",AM);
- obj.put("grade",vathmos);
- }
- //else
- //{
- // System.out.println("bgike");
- // sql3 ="insert into grade values('"+vathmos+"','"+Mathima_id+"','"+Mathitis_id+"')";
- /// stmt.executeUpdate(sql3);
- //obj.put("Mathima",math_name );
- //obj.put("AM",AM);
- //obj.put("grade",vathmos);
- //}
- rs.close();
- stmt.close();
- conn.close();
- return obj.toString();
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment