{"id":1411,"date":"2013-08-16T22:04:17","date_gmt":"2013-08-16T10:04:17","guid":{"rendered":"http:\/\/www.zoyinc.com\/?p=1411"},"modified":"2013-09-29T19:47:36","modified_gmt":"2013-09-29T06:47:36","slug":"problems-with-single-quotes-jsp-and-postgresql","status":"publish","type":"post","link":"http:\/\/www.zoyinc.com\/?p=1411","title":{"rendered":"Problems with single quotes, JSP and PostgreSQL"},"content":{"rendered":"<p>I have a small web app that updates various PostgreSQL tables. I recently updated from Fedora 14 to Fedora 18 and as a result PostgreSQL was updated and I expect JDK as well.<br \/>\nAll of a sudden I notice I couldn&#8217;t update rows which contained strings when the strings had single quotes in them.<br \/>\nI just got back the cryptic error message:<\/p>\n<pre class=\"crayon:false;\">ERROR: syntax error at or near \"\\\" at character 304<\/pre>\n<p>The JSP code looks like:<\/p>\n<pre class=\"lang:java decode:true \" >\t  Connection conn;\r\n      String connectionString=\"jdbc:postgresql:\/\/\"  + dbServer + \":\" + dbPort + \"\/\" +  dbInstance;\r\n      Statement s;\r\n          \r\n      \r\n      int lenstr = newdescription.length();\r\n      String prtlenstr = Integer.toString(lenstr);\r\n      queryString = \"update knowbase set \"+(char)34+\"DESCRIPTION\"+(char)34+\" = ?, \"+(char)34;\r\n      queryString += \"SUBJECT\"+(char)34+\" = ?, \"+(char)34+\"PRODUCTID\"+(char)34+\" = ?, \";\r\n      queryString += (char)34+\"PRIVATE\"+(char)34+\" = ?\";\r\n      queryString = queryString + \" where rtrim(\"+(char)34+\"TIMESTAMP\"+(char)34+\") = rtrim('\" + tstamp + \"')\";\r\n      queryStringLongSave = queryString;\r\n      try {\r\n         DriverManager.registerDriver(new org.postgresql.Driver());\r\n         conn=DriverManager.getConnection(connectionString, dbUserName, dbPassword );\r\n          \r\n         PreparedStatement pstat = conn.prepareStatement( queryString );\r\n         java.io.ByteArrayInputStream bais = null;\r\n         bais = new java.io.ByteArrayInputStream(newdescription.getBytes());\r\n         pstat.setAsciiStream(1, bais, bais.available());\r\n         pstat.setString(2, newsubject);\r\n         pstat.setInt(3, newproductid);\r\n         pstat.setInt(4, newprivate);\r\n         pstat.executeUpdate();\r\n      }\r\n      catch(Exception e){\r\n         updateExeception = \"&lt;br&gt;Error results.jsp e =\" + e.getMessage() + \"&lt;br&gt;&lt;br&gt;\";\r\n      };\r\n<\/pre>\n<h2>Resolution<\/h2>\n<p>After a very long time of searching and a fair share of guesswork it turned out all I needed to do was upgrade my PostgreSQL JDBC drivers to the latest to match the DB version.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a small web app that updates various PostgreSQL tables. I recently updated from Fedora 14 to Fedora 18 and as a result PostgreSQL was updated and I expect JDK as well. All of a sudden I notice I couldn&#8217;t update rows which contained strings when the strings had single quotes in them. I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1424,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[156,155],"tags":[100,309,159,157,158],"class_list":["post-1411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-programming","tag-error","tag-java","tag-jdbc","tag-jsp","tag-postgresql"],"_links":{"self":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/1411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1411"}],"version-history":[{"count":27,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/1411\/revisions"}],"predecessor-version":[{"id":1584,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/1411\/revisions\/1584"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/media\/1424"}],"wp:attachment":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1411"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}