File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes EJB connect with Mysql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB connect with Mysql" Watch "EJB connect with Mysql" New topic
Author

EJB connect with Mysql

Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
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


SCJP 1.4/SCBCD/SCWCD<br />MCSE<br />IBM professional XML Developer<br />SAP certified Development Consultant (NetWeaver portal and Web JavaApplication)
Vladimir Ergovic
Ranch Hand

Joined: Apr 22, 2001
Posts: 63
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)


Vladimir Ergovic
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
Thanks, Vladimir,
I think it is clear to me about it.
Have a nice weekend,
Roy
 
 
subject: EJB connect with Mysql
 
Similar Threads
how to connect different datasources using multiple datasource using ENC
JDBC: Implementation of javax.sql.DataSource in Connect/J ?
Very slow to hook up to MySQL db
how to connect multiple datasource using ENC in jboss
Need help urgent