如何使用API在数据库中搜索人员。

huangapple 未分类评论48阅读模式
英文:

How to search for a person in a datbase using an API

问题

   @GET
   @Path("/seeuser/{idnum}")
   @Produces(MediaType.APPLICATION_JSON)
   public Response seeuser(@PathParam("idnum") String idnum) {
       String responseString = "{\"status_code\":0}";
       StringBuilder crunchifyBuilder = new StringBuilder();
       try {
           String jsonString = crunchifyBuilder.toString();
           Map<String, String> myMap = gson.fromJson(jsonString, mapType);
           String handle = myMap.get("handle");
           Map<String,String> teamMap = Launcher.dbEngine.seeuser(idnum, handle);
           responseString = Launcher.gson.toJson(teamMap);
       } catch (Exception ex) {
           StringWriter sw = new StringWriter();
           ex.printStackTrace(new PrintWriter(sw));
           String exceptionAsString = sw.toString();
           ex.printStackTrace();
           return Response.status(500).entity(exceptionAsString).build();
       }
       return Response.ok(responseString)
               .header("Access-Control-Allow-Origin", "*").build();
   }

DBEngine.java

public Map<String,String> seeuser(String idnum, String handle) {
       Map<String,String> userIdMap = new HashMap<>();

       PreparedStatement stmt = null;
      Integer id = Integer.parseInt(idnum);
       try
       {
           Connection conn = ds.getConnection();
           String queryString = null;
           queryString = "SELECT * FROM Identity WHERE handle = ?";
          stmt = conn.prepareStatement(queryString);
          stmt.setInt(1,id);

           ResultSet rs = stmt.executeQuery();
           while (rs.next()) {
               handle = rs.getString("handle");
               String pass = rs.getString("pass");
               String fullname = rs.getString("fullname");
               String location = rs.getString("location");
               String email = rs.getString("email");
               String bdate = rs.getString("bdate");
               userIdMap.put("handle", handle);
               userIdMap.put("pass", pass);
               userIdMap.put("fullname", fullname);
               userIdMap.put("location", location);
               userIdMap.put("email", email);
               userIdMap.put("bdate", bdate);
           }
           rs.close();
           stmt.close();
           conn.close();
       }
       catch(Exception ex)
       {
           ex.printStackTrace();
       }
       return userIdMap;
   }

请注意,由于您要求只翻译代码部分,我已经将代码翻译为中文,但保留了代码中的变量名、函数名和注释等内容的原样。如果您需要进一步的解释或帮助,请随时提问。

英文:

I am writing a HTTP-based RESTful API SMP and I need to write an API that looks up a users with the same handle as the input and returns all the user information, so the entire row. But right now it is not returning anything. I cannot find the problem, any suggestions on what it might be? Thank you for any help!

/api/seeuser        // find a user and give information
// Input: curl -d &#39;{&quot;handle&quot;:&quot;@cooldude42&quot;, &quot;password&quot;:&quot;mysecret!&quot;}&#39; -H &quot;Content-Type: application/json&quot; -X POST http://localhost:9990/api/seeuser/2 (Links to an external site.)
// 2 = Identity.idnum
// Output: {&quot;status&quot;:&quot;1&quot;, &quot;handle&quot;:&quot;@carlos&quot;, &quot;fullname&quot;:&quot;Carlos Mize&quot;, &quot;location&quot;:&quot;Kentucky&quot;, &quot;email&quot;:carlos@notgmail.com&quot;, &quot;bdate&quot;:&quot;1970-01-26&quot;,&quot;joined&quot;:&quot;2020-04-01&quot;}
// Output: {}. // no match found, could be blocked, user doesn&#39;t know. [EDIT: 04/14]
// etc.

API.java

   @GET
   @Path(&quot;/seeuser/{idnum}&quot;)
   @Produces(MediaType.APPLICATION_JSON)
   public Response seeuser(@PathParam(&quot;idnum&quot;) String idnum) {
       String responseString = &quot;{\&quot;status_code\&quot;:0}&quot;;
       StringBuilder crunchifyBuilder = new StringBuilder();
       try {
           String jsonString = crunchifyBuilder.toString();
           Map&lt;String, String&gt; myMap = gson.fromJson(jsonString, mapType);
           String handle = myMap.get(&quot;handle&quot;);
           Map&lt;String,String&gt; teamMap = Launcher.dbEngine.seeuser(idnum, handle);
           responseString = Launcher.gson.toJson(teamMap);
       } catch (Exception ex) {
           StringWriter sw = new StringWriter();
           ex.printStackTrace(new PrintWriter(sw));
           String exceptionAsString = sw.toString();
           ex.printStackTrace();
           return Response.status(500).entity(exceptionAsString).build();
       }
       return Response.ok(responseString)
               .header(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;).build();
   }

DBEngine.java

public Map&lt;String,String&gt; seeuser(String idnum, String handle) {
       Map&lt;String,String&gt; userIdMap = new HashMap&lt;&gt;();

       PreparedStatement stmt = null;
      Integer id = Integer.parseInt(idnum);
       try
       {
           Connection conn = ds.getConnection();
           String queryString = null;
           queryString = &quot;SELECT * FROM Identity WHERE handle = ?&quot;;
          stmt = conn.prepareStatement(queryString);
          stmt.setInt(1,id);

           ResultSet rs = stmt.executeQuery();
           while (rs.next()) {
               handle = rs.getString(&quot;handle&quot;);
               String pass = rs.getString(&quot;pass&quot;);
               String fullname = rs.getString(&quot;fullname&quot;);
               String location = rs.getString(&quot;location&quot;);
               String email = rs.getString(&quot;email&quot;);
               String bdate = rs.getString(&quot;bdate&quot;);
               userIdMap.put(&quot;handle&quot;, handle);
               userIdMap.put(&quot;pass&quot;, pass);
               userIdMap.put(&quot;fullname&quot;, fullname);
               userIdMap.put(&quot;location&quot;, location);
               userIdMap.put(&quot;email&quot;, email);
               userIdMap.put(&quot;bdate&quot;, bdate);
           }
           rs.close();
           stmt.close();
           conn.close();
       }
       catch(Exception ex)
       {
           ex.printStackTrace();
       }
       return userIdMap;
   }

答案1

得分: 0

我假设句柄被传递给方法Launcher.dbEngine.seeuser(idnum, handle)时为null或空。

变量'crunchifyBuilder' 最初是一个新的StringBuilder(),因此是空的。然后代码按照以下方式进行:

String jsonString = crunchifyBuilder.toString();
Map<String, String> myMap = gson.fromJson(jsonString, mapType);
String handle = myMap.get("handle");

我不确定上述代码是否会将句柄设置为任何值,除非我漏掉了什么?

英文:

I am assuming the handle is being passed as null or empty to the method Launcher.dbEngine.seeuser(idnum, handle).

The variable 'crunchifyBuilder' started as a new StringBuilder() which would be empty. Then the code follows as below

String jsonString = crunchifyBuilder.toString();
Map&lt;String, String&gt; myMap = gson.fromJson(jsonString, mapType);
String handle = myMap.get(&quot;handle&quot;);

I'm not sure the above code would set the handle to any value, unless I am missing something?

huangapple
  • 本文由 发表于 2020年5月4日 01:29:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61578817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定