This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB connect with Mysql

 
Roy Huang
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I want to use an entity Bean (Bean-Managed Persistence) to connect with the mysql DB, how should I configure the Datasource or the JDBC driver?
Like:
try{
.....
ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(
"java:comp/env/jdbc/<dbname>");
con = ds.getConnection();
....}

where should I give them teh correct JDBC driver:
---org.git.mm.mysql.Driver
and the datasource:
---("jdbc:mysql://host ort/<dbname>?use=..?password=...")

Thanks,
Roy
 
Vladimir Ergovic
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should set DataSource somewhere in your app server by editing xml files or clicking some GUI.
Then you just see JNDI name:
Here is the sample for WebSphere but it works for Jboss !
Connection con = null;
String dsName = EnviromentUtil.getEnvString("nameDS");
try {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dsName);
con = ds.getConnection();
} catch (NamingException nex) {
throw new SQLException("Custom: Unable to get connection from DS");
}

Enviroment for that entity has
<env-entry>
<env-entry-name>nameDS</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>jdbc/Arhiv</env-entry-value>
</env-entry>

where jdbc/Arhiv is JNDI datasource name (app server dependant configuration)
 
Roy Huang
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Vladimir,
I think it is clear to me about it.
Have a nice weekend,
Roy
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic