title: Inserting Postgres' json column using wildfly
body: <p>I'm running a web application which is a rest API and I'm using wildfly 9.0.2 and PostgreSQL. The database connection is using the latest jdbc4.2 driver and the jre is oracle's jr8.</p>
  
  <p>I installed the jdbc following this <a href="http://www.mastertheboss.com/jboss-server/jboss-datasource/configuring-a-datasource-with-postgresql-and-jboss-wildfly" rel="nofollow">topic</a>, and got it running perfectly so far I installed the datasource using the wildfly admin console.</p>
  
  <p>So the issue arrived when I attempted to insert a json field. I quickly realized JDBC doesn't have a direct method to insert a json field as it has for let's say String, Integer, Timestamp etc. So I find this solution online somewhere I don't remember now:</p>
  
  <pre><code>oPGobject jsonObject = new PGobject();
  jsonObject.setType("json");
  jsonObject.setValue(new Gson().toJson(ctrlClienteComunicacao));
  preparedStatement.setObject(++i,jsonObject);
  </code></pre>
  
  <p>This solution works fine as I tried it running from a simple main class and I can insert any json I want, but when I tried this code while running on wildfly I got this class not found error:</p>
  
  <pre><code>java.lang.NoClassDefFoundError: org/postgresql/util/PGobject
  </code></pre>
  
  <p>My first atempt was to remove the provided attribute from the JDBC library so it would be included it in the .war. And then I got a new error:</p>
  
  <pre><code>org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.postgresql.util.PGobject. Use setObject() with an explicit Types value to specify the type to use.
  </code></pre>
  
  <p>After a few more attempts I realized that including the jar in the war makes wildfly deploy it as a driver. </p>
  
  <pre><code>[org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) WFLYJCA0018: Started Driver service with driver-name = VirtualForum.war_org.postgresql.Driver_9_4
  </code></pre>
  
  <p>So I tried a different thing, while the war was deployed, I changed the datasource from the original pgsql driver to this one started during deployment of the app and it <strong>works!</strong> I can insert json fields.</p>
  
  <p>But I don't think this is a proper solution as I would prefer it using the driver installed on wildfly.</p>
  
  <p>I apologize for my english and the question layout this is my very first question.</p>