Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用rest web服务、queryparam显示特定用户的详细信息_Java_Web Services_Query Parameters - Fatal编程技术网

Java 使用rest web服务、queryparam显示特定用户的详细信息

Java 使用rest web服务、queryparam显示特定用户的详细信息,java,web-services,query-parameters,Java,Web Services,Query Parameters,我正在用java制作一个登录表单。RESTfulWeb服务。我已经完成了登录和注册。我在这里输入车牌号。我想根据输入的车牌号检索数据。我这里只有扫描的车牌号码,如果它是在数据库中,但我不知道如何显示它的细节。这是我的密码。我很困惑。我不知道怎么做 Constants.java package com.taxisafe.connection; public class Constants { public static String dbClass = "com.mysql.jdbc.Drive

我正在用java制作一个登录表单。RESTfulWeb服务。我已经完成了登录和注册。我在这里输入车牌号。我想根据输入的车牌号检索数据。我这里只有扫描的车牌号码,如果它是在数据库中,但我不知道如何显示它的细节。这是我的密码。我很困惑。我不知道怎么做

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;
}

}
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();
}

}
JsonConstruction.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;
}

}
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;
}   

}
请帮助我如何显示车牌号的详细信息