In this post I'll look at setting up a data source for JBOSS so that you can use a JNDI lookup to connect to your database from your web application. It's really easy and takes no time at all. JBOSS includes some example datasource configurations under JBOSS_HOME/docs/examples/jca that you can copy and edit for your own purposes.
I copied the hsqldb-ds.xml to my server profile under JBOSS_HOME/server/all/deploy and edited it as follows :
beerDS jdbc:h2:tcp://localhost/~/BEER org.h2.jdbcx.JdbcDataSource sa 5 20 0 32
H2 is compatible with the HSQLDB drivers but they recommend using the H2 driver so thats the first thing I changed.
I then changed the connection url and specified my database, “BEER” in this case.org.h2.jdbcx.JdbcDataSource
The configuration shown also sets up a connection pool which should help optimise performance.jdbc:h2:tcp://localhost/~/BEER
Before you can use H2 from within your web application you need to make sure the H2*.jar is on the classpath. I copied my h2*.jar to JBOSS_HOME/common/lib.
Ensure your database is started and use the following code to get a handle to a connection you can then work with in your servlet code:
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/beerDS");
Connection connection = ds.getConnection();
You can then use the connection to query the database using sql or use JPA persistence and the JDNI name java:/beerDS.
These examples use my Beer database I set up in the previous post. This script will allow you to get a H2 sample database up and running. See my previous post for more details
2 comments:
thanks, helped to config Oracle
helped me to configure oracle
Post a Comment