Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 2FE48862DB7 for ; Wed, 8 Feb 2012 16:28:51 -0400 (AST) Received: from mail63.csoft.net ([205.205.214.4]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RvE8I-0008ID-7o for pgsql-bugs@postgresql.org; Wed, 08 Feb 2012 20:28:51 +0000 Received: from mail63.csoft.net (localhost [127.0.0.1]) by mail63.csoft.net (Postfix) with ESMTP id 4E98C28426; Wed, 8 Feb 2012 15:28:37 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=csoft.net; h=date:from:to :cc:subject:in-reply-to:message-id:references:mime-version :content-type; s=sel1; bh=9CXuIII52oBI1IEYXe5SZEatWls=; b=1bFj2u IcTZcbQn5g1OZzWP9UVhkf6SwYMXDKKR2yWFrLI4AeI8Mqr4tl8vDBqXj9L4KHer kUnlp3C2Y386NBPkGqRY5cW22rgoTWLynSx2qPMLELQ0IkcNygYtcnL82VZjak+9 RqZXPCY4EAURnMbylyyHyMdVMswYQps1wTKD0= Received: from leary.csoft.net (leary.csoft.net [205.205.214.5]) by mail63.csoft.net (Postfix) with ESMTP id 4BE2B28424; Wed, 8 Feb 2012 15:28:37 -0500 (EST) Received: by leary.csoft.net (Postfix, from userid 1112) id 40A61A259; Wed, 8 Feb 2012 15:28:37 -0500 (EST) Date: Wed, 8 Feb 2012 15:28:37 -0500 (EST) From: Kris Jurka X-X-Sender: books@leary.csoft.net To: rajesh4.t@tcs.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6445: PreparedStatement.setObject(1,java.util.String) In-Reply-To: Message-ID: References: User-Agent: Alpine 2.00 (BSO 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/81 X-Sequence-Number: 32424 On Wed, 8 Feb 2012, rajesh4.t@tcs.com wrote: > The following bug has been logged on the website: > > Bug reference: 6445 > PostgreSQL version: 8.4.3 > > > I have a table in PostgreSQL database > > CREATE TABLE newtest > ( > id numeric(6), > name character varying(120), > email character varying(120) > ) > > I am using PreparedStatement so that i can insert data into newtest table > > INSERT INTO NewTest (ID,NAME,EMAIL ) values (?,?,? ) > > Actual query fired -INSERT INTO NewTest (ID,NAME,EMAIL ) values ('1','Rajesh > ','rajesh4.t@abs.com' ) > > Please note I am reading data from an excel sheet and > datacell[eachCell].getContents() is of type String..prepareStatement > setObject is complaining that I am sending String datatype which cannot be > casted to bigint or numeric,but that is the job of setObject right to decide > the target datatype and cast accordingly..please reply any > solution..Parameter to setObject(can be anything) so I cannot cast the data > to appropriate format before sending the data > It is not the job of setObject to determine the correct conversion to the column type. The setObject javadoc says, "The JDBC specification specifies a standard mapping from Java Object types to SQL types. The given argument will be converted to the corresponding SQL type before being sent to the database." So it is solely looking at the Java type of the object passed to it and converting that to a SQL type. So you pass it a String and it converts it to a varchar which is appropriate. If you want setObject to do a conversion to a different type, that is the reason for the additional setObject variant which takes a target sql type to convert to, but that doesn't help your situation where you don't know what the target type is. The Postgresql JDBC driver offers three possible workarounds. 1) Use getParameterMetaData to try and determine the target types. 2) Use setObject(int, Object, Types.OTHER) to indicate that you don't know that the Java type you are passing is correct. 3) Use the stringtype=unspecified URL parameter to indicate that all String bindings may not really be strings. Kris Jurka