如何更正此错误?

时间:2014-04-30 17:12:03

标签: java android sql jax-rs

我正在开发Android应用程序,我正在使用RestFul Webservices.I已经使用multiPartEntity将数据从客户端发送到服务器。我在客户端没有任何错误。但我的服务器端代码有错误你帮我吗?

我的客户端代码是:

                    multipart.addPart("hotelName",new StringBody(hotelName));
                    multipart.addPart("userName",new StringBody(userName));
                    multipart.addPart("password",new StringBody(password));
                    multipart.addPart("tinNumber",new StringBody(tinNumber));
                    multipart.addPart("Address",new StringBody(Address));
                    multipart.addPart("mobile_No",new StringBody(mobile_No));
                    multipart.addPart("email_Id",new StringBody(email_Id));
                    multipart.addPart("star_Rate",new StringBody(star_Rate));                       
                    multipart.addPart("imagePath",new InputStreamBody(getContentResolver().openInputStream(Uri.parse("file://"+imagePath)),imageName+".jpg"));

                    httppost.setEntity(multipart);
                    response = httpclient.execute(httppost);

我的服务器端代码

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public void postHotel(MultiPart hotelParams)
        throws SQLException, FileNotFoundException, IOException {

            stmt.executeUpdate("insert into hotel (name,user_name,password,tin_number,address,mobile_no,email,rating,hotel_logo) values('"
            +hotelParams.getBodyParts().get(0)
            + "','"
            + hotelParams.getBodyParts().get(1)
            + "','"
            + hotelParams.getBodyParts().get(2)
            + "',"
            + hotelParams.getBodyParts().get(3)
            + ",'"
            + hotelParams.getBodyParts().get(4)
            + "',"
            + hotelParams.getBodyParts().get(5)
            + ",'"
            + hotelParams.getBodyParts().get(6)
            + "','"
            + hotelParams.getBodyParts().get(7) + "','" + hotelParams.getBodyParts().get(8) + "');");

我的错误如下:

SERVE: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near '.multipart.FormDataBodyPart@9d2f26,'com.sun.jersey.multipart.FormDataBodyPart@18' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1664)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1583)
    at com.orderfree.Resource.HotelRegsiter.postHotel(HotelRegsiter.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

请帮助我的朋友

3 个答案:

答案 0 :(得分:0)

尝试打印hotelParams.getBodyParts().get(0)

的值时出现sql语法错误

hotelParams.getBodyParts().get(1)

1.试着检查你是否有字符串。

2.尝试检查sql查询之间的空格

3.尝试通过这样的方式编辑你的代码

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public void postHotel(@FormDataParam("user_name") String userName
                   @FormDataParam("password") String password)
        throws SQLException, FileNotFoundException, IOException {

            stmt.executeUpdate("insert into hotel(user_name,password)values('"
            +userName
            + "','"
            +password
            + "');

答案 1 :(得分:0)

为什么要为此目的使用multipart? 请仅使用两个参数,用户和密码。我建议您从这个简单的解决方案开始:

   @Path("/user/{username}/{password}")
   public void postHotel(@PathParam("username") String username, @PathParam("password") String password) {
        stmt.executeUpdate("insert into hotel(username,password)values('"
        +hotelParams.getBodyParts().get(0)
        + "','"
        + hotelParams.getBodyParts().get(1)
        + "');

答案 2 :(得分:0)

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public void postHotel(MultiPart hotelParams)
        throws SQLException, FileNotFoundException, IOException {

            stmt.executeUpdate("insert into hotel(user_name,password)values('"
            +hotelParams.getBodyParts().get(0)
            + "','"
            + hotelParams.getBodyParts().get(1)
            + "'");