views:

189

answers:

1

I was using spring jdbc template to insert some data into the database and I was getting this error.

Here is my code :

JdbcTemplate insert = new JdbcTemplate(dataSource);
        for(ResultType result : response.getResultSet().getResult())
        {
            Object[] args = new Object[] {result.getAddress(), result.getCity(), result.getState(), result.getPhone(), result.getLatitude(), result.getLongitude(),result.getRating().getAverageRating(), result.getRating().getAverageRating(), result.getRating().getTotalRatings(), result.getRating().getTotalReviews(), result.getRating().getLastReviewDate(), result.getRating().getLastReviewIntro(), result.getDistance(), result.getUrl(), result.getClickUrl(), result.getBusinessUrl(), result.getBusinessClickUrl()};
            insert.update("INSERT INTO data.carwashes ( address, city, state, phone, lat, lng, rating, average_rating, total_ratings, total reviews, last_review_date, last_review_intro, distance, url, click_url, business_url, business_click_url, category_id, zipcode_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,96925724,78701)", args);
        }

Quite lengthy code.. but, basically it gets the value from a object and sticks it to a array and passed that array to insert method of jdbc template.

Any help will be appreciated.

+3  A: 

My guess is that one of the items in your arg array is of a type not recognised by JdbcTemplate, (i.e. it's not a String, a Date, and so on), and so it's calling setObject() on the statement. The driver will then try to serialize the argument into a binary, discovers it's not serializable, and throws the exception.

Make sure the arguments in the array are all what you think they should be, e.g. they shouldn't be instances of your own classes, but should be standard Java types.

skaffman
+1: I also believe this. Btw: To insert data in a for loop isn't very efficient. You should consider `JdbcTemplate`s `batchUpdate(sql, pss)` method.
Espen
Thanks Espen. I'll do that. This was more like a test run to see whether all pieces fit together. I'll try these solutions and see.
Vanchinathan Chandrasekaran