Display details on specific user using rest web service, queryparam

121 views Asked by At

I am making a log in form on java. Restful web service. I have done logging in and in registration. I have here inputting plate number. I want to retrieve data based on the inputted plate number. I have here the scanning only of the plate number if it is in the database, but I dont know how to display the details of it. Here's my code. I'm so confused. I don't know how to do it.

Constants.java

package com.taxisafe.connection;

public class Constants {
public static String dbClass = "com.mysql.jdbc.Driver";
private static String dbName= "taxisafe";
public static String dbUrl = "jdbc:mysql://localhost:3306/"+dbName;
public static String dbUsername = "root";
public static String dbPassword = "";
}

DatabaseConnection.java

package com.taxisafe.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.taxisafe.objects.Objects;


public class DatabaseConnection {

@SuppressWarnings("finally")
public static Connection createConnection() throws Exception {
    Connection koneksyon = null;
    try {
        Class.forName(Constants.dbClass);
        koneksyon = DriverManager.getConnection(Constants.dbUrl, Constants.dbUsername, Constants.dbPassword);
    } catch (Exception e) {
        throw e;
    } finally {
        return koneksyon;
    }
}

//CHECK FOR LOGIN
public static boolean checkUser(String username, String password) throws Exception {  //checkLogin to checkUser
    boolean UserRecorded = false;
    Connection konek = null;
    try {
        try {
            konek = DatabaseConnection.createConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Statement statement = konek.createStatement();
        String code = "SELECT * FROM user WHERE username = '" + username + "' AND password=" + "'" + password + "'";
        ResultSet rs = statement.executeQuery(code);
        while (rs.next()) {
            UserRecorded = true;
        }
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (konek != null) {
            konek.close();
        }
        throw e;
    } finally {
        if (konek != null) {
            konek.close();
        }
    }
    return UserRecorded;
}

// REGISTER USER
public static boolean registertheUser(String name, String username, String email, String password) throws SQLException, Exception { //inserUser - registertheUser
boolean insertUser = false; //insertStatus - insertUser
Connection konek = null;
try{
    try{
        konek = DatabaseConnection.createConnection();
    } 
    catch (Exception e){
        e.printStackTrace();
    }
    Statement statement = konek.createStatement();
            String code = "INSERT into user(name, username, emailaddress, password) values('"+name+ "',"+"'" + username + "','"+ email + "','" + password + "')";
    int dbrecord = statement.executeUpdate(code);
    if (dbrecord > 0){
        insertUser = true;
    }
} catch (SQLException sqle){
    throw sqle;
} catch (Exception e){
    if (konek !=null){
        konek.close();
    }
    throw e;
} finally{
    if (konek !=null){
        konek.close();
    }
} return insertUser;

}

//CHECK PLATE NUMBER
public static boolean checkPlate(String platenumber) throws Exception {  //checkLogin to checkUser
    boolean PlateRecorded = false;
    Connection konek = null;
    try {
        try {
            konek = DatabaseConnection.createConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Statement statement = konek.createStatement();
        String code = "SELECT * FROM taxi WHERE taxi_plate_no = '" + platenumber+ "'";
        ResultSet rs = statement.executeQuery(code);
        while (rs.next()) {
            PlateRecorded = true;
        }
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (konek != null) {
            konek.close();
        }
        throw e;
    } finally {
        if (konek != null) {
            konek.close();
        }
    }
    return PlateRecorded;
}

}

JsonConstruction.java

package com.taxisafe.json;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class JsonConstruction {

public static boolean isNotNull(String text){
    return text !=null && text.trim().length() >=0 ? true : false;
}

public static String JSONResponse(String tag, boolean status){
    JSONObject object = new JSONObject();
    try{
        object.put("tag", tag);
        object.put("status", new Boolean(status));
    } catch (JSONException e){

    } return object.toString();
}

public static String JSONResponse(String tag, boolean status, String errorMessage){
    JSONObject object = new JSONObject();
    try{
        object.put("tag", tag);
        object.put("status", new Boolean(status));
        object.put("errorMessage", errorMessage);
    } catch (JSONException e){

    } return object.toString();
}

}

PlateNumberCheck.java

package com.taxisafe.server;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.sun.corba.se.impl.util.Utility;
import com.taxisafe.connection.DatabaseConnection;
import com.taxisafe.json.JsonConstruction;

@Path("platecheck")  //for the url

public class PlateNumberCheck {
@GET 
//To get the full url : http://Ipaddress:portnumber/@path/@getPath 
@Path("/magcheck")
@Produces(MediaType.APPLICATION_JSON) 
//Produces is for the response of JSON.

public String magcheck(@QueryParam("platenumber") String platenumber){
    String sagot = "";
    if(checkInput(platenumber)){
        sagot = JsonConstruction.JSONResponse("checked", true);
    } else{ 
        sagot = JsonConstruction.JSONResponse("checked", false, "Not in the database");
    }
    return sagot;
}

private boolean checkInput (String platenumber){
    System.out.println("Check Input");
    boolean output = false;
    if(JsonConstruction.isNotNull(platenumber)){
        try{
            output = DatabaseConnection.checkPlate(platenumber);
        } catch (Exception e){
            output = false;
        }
    } else{
        output = false;
    }

    return output;
}   

}

Please help me on how to display the details of the plate number.

0

There are 0 answers