程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 第一次接觸ejb

第一次接觸ejb

編輯:關於JAVA

這是我第一次使用EJB,以前從來沒有接觸過,也許是因為沒有幾乎,也許是因為敬畏。

在青州實習有機會使用EJB了,實際上是可用可不用(培訓課程的網上提交),但我認為這是個學習的機會,就開始獨自摸爬滾打了。

我采用Eclipse3.0+JBoss3.2.5+JBossIDE1.3.30+Sybase Adaptive SQL Anywhere9.0的模式。

沒有太多的難點,但是還是折騰了2天多。

問題1:下載了JBoss3.2.5和JBossIDE1.3.30,但是如何安裝這個IDE插件到Eclipse當中呢?首先詢問Eclipse插件開發經驗的人,說插件一般拷貝到plugin目錄下就可以了,然後customize perspective裡面選擇相應的選項就可以了,比如(New,open perspective和show view,New是說可以new出相應的工程或者文件,show view就是可以生成一個相應的視圖)。但是我把JBossIDE解壓拷貝到plugin中後,在customize的時候只發現了幾個能在show view中出現的選項,和JBoss網站上下載的tutorial上說的有新建工程不同。所以郁悶了一陣。解決的方法是install手冊上說應該用Eclipse的Help->Software Updates->Find and install...菜單,然後添加相應JBossIDE的更新網站就可以了。按照說明果然成功。

後來又進行了default sever的設置,參見JBossIDE tutorial上的最後部分。

問題2:連接http://localhost:8080/ 後的相應的jsp網頁報告找不到co.sun.tools.javac.Main,報告可能的原因是該類不在JAVA_HOME目錄下。我首先在環境變量中設置但是沒有作用,後來在Eclipse的Window->Preferences菜單下的Java->Installed JREs中編輯j2re1.4.2裡面Add External JARs添加了tools.jar,然後就好使了。

問題3:如何將Sybase ASA(Adaptive sql anywhere)加入JBoss中。我參考了比較多的資料。在Huihoo.org(http://www.huihoo.org/jboss/)上找的一篇文章還不錯。如下:

Jboss3.0-tomcat4.03的配置-4(數據庫的配置)

(灰狐動力-中間件技術論壇 by huihoo.com Etre)

Jboss3.0-Tomcat4.03的數據庫的配置(以Oracle為例)

一、 請將classes12.zip加入classpath中去。

二、請參況hsqldb-service.xml文件編寫Oracledb-service.xml文件

請打開server\default\deploy目錄下的hsqldb-service.xml文件,這就是一個Jboss3.0_Tomcat4.03下實現數據庫連接的例子,它是連接hsql數據庫的它的內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- JBoss Server Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!-- $Id: hsqldb-service.xml,v 1.2.2.5 2002/06/23 21:25:58 d_jencks Exp $ -->
<server>
<!-- ==================================================================== -->
<!-- New ConnectionManager setup for default hsql dbs -->
<!-- Build jmx-api (build/build.sh all) and view for config documentation -->
<!-- ==================================================================== -->
<mbean code="org.jboss.resource.connectionmanager.LocalTxConnectionManager" name="jboss.jca:service=LocalTxCM,name=hsqldbDS">
<!-- Include a login module configuration named HsqlDbRealm.
Update your login-conf.xml, here is an example for a
ConfiguredIdentityLoginModule:
<application-policy name = "HsqlDbRealm">
<authentication>
<login-module code = "org.jboss.resource.security.ConfiguredIdentityLoginModule" flag = "required">
<module-option name = "principal">sa</module-option>
<module-option name = "userName">sa</module-option>
<module-option name = "password"></module-option>
<module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=hsqldbDS</module-option>
</login-module>
</authentication>
</application-policy>
NOTE: the application-policy name attribute must match
SecurityDomainJndiName, and the
module-option name = "managedConnectionFactoryName"
must match the object name of the ConnectionManager you are configuring here.
-->
<!--uncomment out this line if you are using the DB2DbRealm above
<attribute name="SecurityDomainJndiName">HsqlDbRealm</attribute>
-->
<depends optional-attribute-name="ManagedConnectionFactoryName">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=hsqldbDS">
<attribute name="JndiName">DefaultDS</attribute>
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL" type="java.lang.String">jdbc:hsqldb:hsql://localhost:1476</config-property>
<config-property name="DriverClass" type="java.lang.String">org.hsqldb.jdbcDriver</config-property>
<config-property name="UserName" type="java.lang.String">sa</config-property>
<config-property name="Password" type="java.lang.String"></config-property>
</properties>
</attribute>
<!--Below here are advanced properties -->
<!--hack-->
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
<depends>jboss:service=Hypersonic</depends>
</mbean>
</depends>
<depends optional-attribute-name="ManagedConnectionPool">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.JBossManagedConnectionPool" name="jboss.jca:service=LocalTxPool,name=hsqldbDS">
<attribute name="MinSize">0</attribute>
<attribute name="MaxSize">50</attribute>
<attribute name="BlockingTimeoutMillis">5000</attribute>
<attribute name="IdleTimeoutMinutes">15</attribute>
<!--criteria indicates if Subject (from security domain) or app supplied
parameters (such as from getConnection(user, pw)) are used to distinguish
connections in the pool. Choices are
ByContainerAndApplication (use both),
ByContainer (use Subject),
ByApplication (use app supplied params only),
ByNothing (all connections are equivalent, usually if adapter supports
reauthentication)-->
<attribute name="Criteria">ByContainer</attribute>
</mbean>
</depends>
<depends optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedConnectionManager</depends>
<depends optional-attribute-name="JaasSecurityManagerService">jboss.security:service=JaasSecurityManager</depends>
<attribute name="TransactionManager">java:/TransactionManager</attribute>
<!--make the rar deploy! hack till better deployment-->
<depends>jboss.jca:service=RARDeployer</depends>
</mbean>
<!-- Moved to end to test anonymous depends -->
<mbean code="org.jboss.jdbc.HypersonicDatabase"
name="jboss:service=Hypersonic">
<attribute name="Port">1476</attribute>
<attribute name="Silent">true</attribute>
<attribute name="Database">default</attribute>
<attribute name="Trace">false</attribute>
</mbean>
</server>

在<!-- -->之間沒有用的一些說明,所以去掉這一些說明後經過改寫一個存取oracle數據庫的服務的XML的文件是Oracledb-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.resource.connectionmanager.LocalTxConnectionManager" name="jboss.jca:service=LocalTxCM,name=oracleDS">
<depends optional-attribute-name="ManagedConnectionFactoryName">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=oracleDS">
<attribute name="JndiName">OracleDS</attribute>
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL" type="java.lang.String">jdbc:oracle:thin:@cuipeng:1521:Ora8</config-property>
<config-property name="DriverClass" type="java.lang.String">oracle.jdbc.driver.OracleDriver</config-property>
<config-property name="UserName" type="java.lang.String">system</config-property>
<config-property name="Password" type="java.lang.String">manager</config-property>
</properties>
</attribute>
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
</mbean>
</depends>
<depends optional-attribute-name="ManagedConnectionPool">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.JBossManagedConnectionPool" name="jboss.jca:service=LocalTxPool,name=oracleDS">
<attribute name="MinSize">0</attribute>
<attribute name="MaxSize">50</attribute>
<attribute name="BlockingTimeoutMillis">5000</attribute>
<attribute name="IdleTimeoutMinutes">15</attribute>
<attribute name="Criteria">ByContainer</attribute>
</mbean>
</depends>
<depends optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedConnectionManager</depends>
<depends optional-attribute-name="JaasSecurityManagerService">jboss.security:service=JaasSecurityManager</depends>
<attribute name="TransactionManager">java:/TransactionManager</attribute>
<depends>jboss.jca:service=RARDeployer</depends>
</mbean>
</server>

請大家對比兩個文件的不同之外,很容易寫出其它數據庫的存取服務了,主要的是下面這個部分

<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=oracleDS">
<attribute name="JndiName">OracleDS</attribute><!--這個地方是JNDI的名字在ejb或jsp中調用的是java:/OracleDS - ->
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL" type="java.lang.String">jdbc:oracle:thin:@cuipeng:1521:ora8</config-property>
<config-property name="DriverClass" type="java.lang.String">oracle.jdbc.driver.OracleDriver</config-property>
<config-property name="UserName" type="java.lang.String">system</config-property>
<config-property name="Password" type="java.lang.String">manager</config-property>
</properties>
</attribute>
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
</mbean>

還有就是各個部分的名字了name=oracleDS

三、測試數據的連接列子(我用的是jsp)

你必需要編寫.war或.ear包來測試一個簡易的做法就是:

一、 在deploy目錄下建立oracle-test.war目錄。

二、在oracle-test.war目錄下建立WEB-INF目錄,其中有兩個文件

jboss-web.xml和web.xml

內容分別是

jboss-web.xml內容是

<jboss-web>
</jboss-web>
web.xml內容是
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
</web-app>

三、在oracle-test.war目錄下建立新文件index.jsp其內容是

<%@page contentType="text/html"%>
<%@ page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext" %>
<h3>Test Oracle DB</h3>
<%
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/OracleDS"); //注意這裡哦和前面對應
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM TAB");
while ( rs.next() ) {
out.println( rs.getString("TNAME") + "<br>");
}
conn.close();
%>

四、請訪問http://localhost:8080/oracle-test

一切都測試OK的,如果有什麼不對的地方請指哦,我也是學習的。這是一個快速的配置方法,在過一些時間我會把每個屬性表示什麼給大家說說.

經我根據摸索Sybase ASA更改後如下:

1 在Sql Anywhere 9目錄的java目錄下找到jdbcdrv.zip,拷貝到jboss-3.2.5的server\default\lib目錄下

2 在jboss-3.2.5\docs\examples\jca目錄下找到sybase-ds.xml拷貝到jboss-3.2.5\server\default\deploy目錄下

3 修改sybase-ds.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- JBoss Server Configuration -->
<!-- -->
<!-- New ConnectionManager setup for Sybase ASE/ASA jConnect driver -->
<!-- Thanks to Marc Zampetti <[email protected]> -->
<!-- This is a minimal example. See the generic example for other stuff -->
<!-- ===================================================================== -->
<!-- $Id: sybase-ds.xml,v 1.1.2.2 2003/09/05 16:38:24 patriot1burke Exp $ -->
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/SybaseDB</jndi-name>
<!-- Sybase jConnect URL for the database.
NOTE: The hostname and port are made up values. The optional
database name is provided, as well as some additinal Driver
parameters.
-->
<connection-url>jdbc:sybase:Tds:Gibeon:2638/QZCFTraining?JCONNECT_VERSION=4</connection-url>
<driver-class>com.sybase.jdbc.SybDriver</driver-class>
<user-name>DBA</user-name>
<password>SQL</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.SybaseExceptionSorter</exception-sorter-class-name>
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
</local-tx-datasource>
</datasources>

4 修改index.jsp文件,如下:

<%@page contentType="text/html"%>
<%@ page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext" %>
<h3>Test Sybase DB</h3>
<%
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/jdbc/SybaseDB"); //注意這裡哦和前面對應
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Trainee");
while ( rs.next() ) {
out.println( rs.getString("name") + "<br>");
}
conn.close();
%>

然後就好使了,但是現在存在的問題是中文問題!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved