Question

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?

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

Was it helpful?

Solution

It is a sql syntax error try to print the values of hotelParams.getBodyParts().get(0)

and hotelParams.getBodyParts().get(1)

1.Try to Check whether you are getting strings or not.

2.Try to check whitespaces in between sql query

3.Try to edit ur code by putting something like this

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

OTHER TIPS

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)
            + "'");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top