简体   繁体   English

如何更正此错误?

[英]How to correct this error?

I am developing android App and I am using RestFul Webservices.I have used multiPartEntity to send data from client side to server.I don't have any error in client side.but I have error in my server side code.can you help me? 我正在开发android app,并且正在使用RestFul Webservices。我已经使用multiPartEntity将数据从客户端发送到服务器。我在客户端没有任何错误。但是我在服务器端代码中有错误。你能帮我吗?

My client side code is: 我的客户端代码是:

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

My Server side Code is 我的服务器端代码是

@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) + "');");

My error is like: 我的错误是这样的:

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)

Please help me friends 请帮我朋友

It is a sql syntax error try to print the values of hotelParams.getBodyParts().get(0) 尝试打印hotelParams.getBodyParts().get(0)的值是SQL语法错误

and hotelParams.getBodyParts().get(1) hotelParams.getBodyParts().get(1)

1.Try to Check whether you are getting strings or not. 1.尝试检查您是否正在获取字符串。

2.Try to check whitespaces in between sql query 2.尝试检查SQL查询之间的空格

3.Try to edit ur code by putting something like this 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
            + "');

Why do you are using multipart for this purpose? 您为什么为此目的使用分段? Please use only two args, user and password. 请仅使用两个参数,用户名和密码。 I recommend to you start with this easy solution: 我建议您从这个简单的解决方案开始:

   @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)
        + "');
@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)
            + "'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM