Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 96AF9B5DC09 for ; Tue, 23 Aug 2011 07:03:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 58552-03 for ; Tue, 23 Aug 2011 10:02:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from claranet-outbound-smtp02.uk.clara.net (claranet-outbound-smtp02.uk.clara.net [195.8.89.35]) by mail.postgresql.org (Postfix) with ESMTP id 14A33B5DC02 for ; Tue, 23 Aug 2011 07:02:53 -0300 (ADT) Received: from [217.158.205.40] (port=49957 helo=BSP1.visitlondon.com) by relay02.mail.eu.clara.net (relay.clara.net [213.253.3.42]:1025) with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) id 1Qvnoo-0006vO-7X (return-path ); Tue, 23 Aug 2011 10:02:50 +0000 Message-ID: <4E537AC9.2090203@londonandpartners.com> Date: Tue, 23 Aug 2011 11:02:49 +0100 From: Chris Wareham User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:6.0) Gecko/20110812 Thunderbird/6.0 MIME-Version: 1.0 To: JavaNoobie CC: Subject: Re: Update+ Prepared Statement Error. References: <1314077696657-4725545.post@n5.nabble.com> In-Reply-To: <1314077696657-4725545.post@n5.nabble.com> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: none X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201108/63 X-Sequence-Number: 22095 On 23/08/11 06:34, JavaNoobie wrote: > Hi all, > Im trying to write a PreparedStatement , to pass an update query as below > . > > query = "UPDATE db_consumer SET > WENEXA_ID=?,RR_NO=?,CONSUMER_NAME=?,RESIDING_VILLAGE=?,CONTACT_NO=?,CONTACT_PERSON=?,REP_DATE=?,STATUS_ID=? > WHERE CONSUMER_ID=?"; > stmt = con.prepareStatement(query); > stmt.setString(1, bean.getWenexa_id()); The bean.getWenexa_id() call returns null? > stmt.setString(2, bean.getRr_number()); > stmt.setString(3, bean.getConsumer_name()); > stmt.setString(4, bean.getResiding_village()); > stmt.setString(5, bean.getContact_no()); > stmt.setString(6, bean.getContact_person()); > if (bean.getRep_date() == null || bean.getRep_date() == "") { > bean.setRep_date(null); > } The check and set of null here is redundant, and you are comparing the identity of the rep_date string with the empty string. Unless the rep_date string is interned this will most likely not work as you expect. The clause should be: if ("".equals(bean.getRep_date())) { bean.setRep_date(null); } Although I have to wonder why are you representing a date in your bean as a String rather than a Date. If it was a Date, then the previous clause would be unnecessary and the next clause could be: if (bean.getRep_date() != null) { stmt.setDate(7, new java.sql.Date(bean.getRep_date().getTime())); } else { stmt.setNull(7, Types.DATE); } That would save you the cost of parsing a date from a string. > if (bean.getRep_date() != null) { > System.out.println("DAte before Insert" + bean.getRep_date()); > dtd2 = df2.parse(bean.getRep_date()); > sqlDate1 = new java.sql.Date(dtd2.getTime()); > } > > > stmt.setDate(7, sqlDate1); > stmt.setInt(8, bean.getStatus()); > stmt.setInt(9, bean.getConsumer_id()); > > System.out.println(stmt.toString()); The toString() call is redundant, an implicit conversion will occur without it. > stmt.executeUpdate(); > > However, I get an error as below. > org.postgresql.util.PSQLException: No value specified for parameter 1. > at > org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:178) > at > org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:246) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388) > at > org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334) > at > com.enzen.cis.dao.DAOConsumerAddEdit.updateDB(DAOConsumerAddEdit.java:253) > > I'm not being able to figure out why this is happening . However, when I try > to run the query printed by the toString() method at the database, it works > perfectly . Am I missing something in the syntax? > Thank you for your time. >