0

そして、ここに私の残りのコードがあります

@Path("Users")
public class ItemsResource extends stu{

  @Context
  private UriInfo context;

  /**
   * Creates a new instance of ItemsResource
   */
  public ItemsResource() {
  }

  /**
   * Retrieves representation of an instance of service.ItemsResource
   * @return an instance of java.lang.String
   */
  @GET
  @Produces("text/plain")
  public String getText() {
      //TODO return proper representation object
      throw new UnsupportedOperationException();
  }

  /**
   * POST method for creating an instance of ItemResource
   * @param content representation for the new resource
   * @return an HTTP response with content of the created resource
   */
   @POST
   @Consumes("text/plain")
   @Produces("text/plain")
   public Response postText(@PathParam("id") int id,@PathParam("Password") String Password,
                        @PathParam("Username") String Username) {
    //TODO

       super.createText(id,Password,Username);
       return Response.created(context.getAbsolutePath()).build();
      // return "success";
   }
  /**
   * Sub-resource locator method for {id}
   */   
}

ここにstu.javaがあります

class stu {

     public String createText(int id,String Username,String Password) {
        //TODO
       try 
       {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","789456");

          String query=  "Insert into users (id,username,password) values('"+id+"','"+Username+"','"+Password+"')";
          PreparedStatement stm = con.prepareStatement(query);         

          stm.executeUpdate(query);
        } catch(ClassNotFoundException e){
           System.err.println ("Sql Exception"+e);
        } catch(SQLException e){
           System.err.println ("Sql Exception"+e);
        }

    return "success";
  } 
}

残りをテストしてデータを投稿すると、データは正常に投稿されますが、データベースで更新されません親切にエラーを教えてください

REST 応答は次のとおりです。

Request: POST localhost:8090/WebApplication16/resources/Users?
timestamp=1346152551629

Status: 201 (Created)

Time-Stamp: Tue, 28 Aug 2012 11:15:51 GMT

Sent:
1,hey,hdhb

Received:


-----------------------------------------------------------------------

Request: localhost:8090/WebApplication16/resources/application.wadl


Status: 200 (OK)

Time-Stamp: Tue, 28 Aug 2012 11:15:41 GMT

Received:

<?xml version="1.0" encoding="UTF-8"?>
   <application xmlns="research.sun.com/wadl/2006/10">
       <doc xmlns:jersey="jersey.dev.java.net/" jersey:generatedBy="Jersey: 1.5 01/14/2011 12:36 PM"/>
       <resources base="localhost:8090/WebApplication16/resources/">
           <resource path="Users">
               <param xmlns:xs="www.w3.org/2001/XMLSchema" name="id" style="template" type="xs:int"/>
               <param xmlns:xs="www.w3.org/2001/XMLSchema" name="Username" style="template" type="xs:string"/>
               <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="Password" style="template" type="xs:string"/>
               <method id="getText" name="GET">
                   <response>
                       <representation mediaType="text/plain"/>
                   </response>
               </method>
               <method id="postText" name="POST">
                   <response>
                       <representation mediaType="text/plain"/>
                   </response>
               </method>
           </resource>
       </resources>
   </application> 

そして、データベースに接続でき、テーブルに新しい行が作成されますが、値は null、null、null です。これで私を助けてください。

4

2 に答える 2

1

データが「正常に投稿」されていないと思います。のよう@PathParamに、にパラメーターがある場合は、注釈を使用します。@Path@Path("Users/{id}")

どのようにパラメータを送信していますか? ブラウザと POST で送信する場合は、アノテーション<form>を使用してみてください。@FormParam詳細については、この質問を参照してください。

于 2012-08-28T12:39:55.027 に答える
0

プリペアドステートメントを使用すると、すぐに値を入力するべきではありません。次を試してください

String query=  "insert into users (id,username,password) values(?,?,?)";
PreparedStatement stm = con.prepareStatement(query); 
stm.setInt(1, id);
stm.setString(2, Username);
stm.setString(3, Password);
stm.executeUpdate();

APIページにこの例があります

また、補足として、あなたの議論に注意してください。メソッド定義にはcreateText(int id,String Username,String Password)がありますが、パスワードとユーザー名を混同して値を渡します。

于 2012-08-28T12:32:39.470 に答える