Você está na página 1de 3

Andreas Martin Blog: how I set up a PostgreSQL ...

https://community.jboss.org/blogs/amartin-blog...

Todas as Pessoas > Andreas Martin > Andreas Martin Blog > 2012 > Fevereiro > 08

Andreas Martin Blog

how I set up a PostgreSQL JDBC Driver on JBoss 7


Postado por Andreas Martin em Andreas Martin Blog em 8/Fev/2012 10:03:15

1.

Download the Postgres Driver which you need as .jar-file (http://jdbc.postgresql.org/download.html) Go to JBoss directory and find modules\org folder. Create two new folders, so you get modules\org\postgresql\main and insert the .jar file there. Create in the same folder a file named module.xml with the content (set in <resource-root path="" > the name of your .jar-le):
01. 02. 03. 04. 05. 06. 07. 08. 09. 10. <?xml version="1.0" encoding="UTF-8"?> <module xmlns="urn:jboss:module:1.0" name="org.postgresql"> <resources> <resource-root path="postgresql-Y.X-Z.jdbcV.jar"/> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> </dependencies> </module>

2.

3.

4.

Edit your standalone.xml and add the following code between the tags <datasources><drivers>
01. 02. 03. 04. 05. <driver name=postgresql module=org.postgresql> <xa-datasource-class> org.postgresql.xa.PGXADataSource </xa-datasource-class> </driver>

5.

Now start the server and check wether he is loading the driver. The console should show something like: INFO [org.jboss.as.connector.subsystems.datasources] JBAS010404: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 8.4). If it nothing showing about your Postgres Driver, check the spelling and the coding (e.g. the quotation marks can be in different code and dont work, can happen if you just copy and paste). Fix this before going on. Go to your standalone.xml and set between <datasources> following code (set your connection-url, usename and password, jndi-name can also be java:/postgresDS, you can do this step through admin console of the server as well):

6.

1 de 11

26-03-2014 14:09

Andreas Martin Blog: how I set up a PostgreSQL ...

https://community.jboss.org/blogs/amartin-blog...

01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46.

<datasource jndi-name="java:jboss/postgresDS" pool-name="postgresDS" enabled <connection-url> jdbc:postgresql://192.z.y.x:5432/yourdatabase </connection-url> <driver-class> org.postgresql.Driver </driver-class> <driver> postgresql </driver> <pool> <min-pool-size> 2 </min-pool-size> <max-pool-size> 20 </max-pool-size> </pool> <security> <user-name> urusername </user-name> <password> urpasswort </password> </security> <validation> <validate-on-match> false </validate-on-match> <background-validation> false </background-validation> <background-validation-millis> 0 </background-validation-millis> </validation> <statement> <prepared-statement-cache-size> 0 </prepared-statement-cache-size> <share-prepared-statements> false </share-prepared-statements> </statement> </datasource>

7. 8.

Start you database server Test the jdbc connection with following jsp-file (set the name of your table and the column names):
01. 02. 03. 04. 05. 06. 07. 08. <%@page import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*"%> <% DataSource ds = null; Connection con = null; Statement stmt = null; InitialContext ic;

2 de 11

26-03-2014 14:09

Andreas Martin Blog: how I set up a PostgreSQL ...


09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

https://community.jboss.org/blogs/amartin-blog...

try { ic = new InitialContext(); ds = (DataSource) ic.lookup("java:/postgresDS"); con = ds.getConnection(); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from tabname"); while (rs.next()) { out.println("<br> " + rs.getString("onecolumn") + " | " + rs.getString("othercolumn")); } rs.close(); stmt.close(); } catch (Exception e) { out.println("Exception thrown :/"); e.printStackTrace(); } finally { if (con != null) { con.close(); } } %>

9.

Done.

The knowledge for this tutorial i got from the following sites (big thank to them!): https://community.jboss.org/wiki/JBossAS7DatasourceConfigurationForPostgresql https://community.jboss.org/thread/168958 http://kousikraj.wordpress.com/2011/11/25/datasource-configuration-setupfor-jboss-as-7-with-example-of-postgresql/ http://blog.xebia.com/2011/07/19/developing-a-jpa-application-on-jboss-as-7/ http://jan.zawodny.pl/blog/2011/07/jboss-7-postgresql-9 https://community.jboss.org/thread/168644
64477 Visualizaes Rtulos:

Classificao de usurio mdio (18 Classificaes)

Minha Pontuao:

17 Comentrios
Javin Paul 27/Out/2012 12:12

This is great compilation, Though information is already available on mentioned threads but you have put together all of them nicely here. Good job and Thank you very much. Javin

3 de 11

26-03-2014 14:09