使用JBoss和PostgreSQL-----快速开发EJB和J2EE Web Application

先安装JSDK,再安装JBoss.
安装JSDK,必须获得一套对应于用户的操作系统的JDK,
我的安装的文件目录是
WINDOWS2000: d:s1studio_jdkj2sdk1.4.1
linux: /root/s1studio_jdk/j2sdk1.4.1
为了用EJB, 需要一个j2ee-1.3.jar或者j2ee-1.2.jar,
如果安装了Sun One Studio 或者 J2EE (www.sun.com )这个文件已经有.
把这个文件放在classpath路径上.
或者使用jboss-j2ee.jar, 安装JBoss后在$JBossclient中可找到.
建议安装Sun One Studio, 用Sun One Studio编译JAVA源程序,
不用设置classpath, 省去不少过程.

安装JBoss:
把JBoss的压缩包解开,放在任一目录上,
我的安装的文件目录是
/dose/jboss-3.0.4_tomcat-4.1.12 (REDHAT8.0)
E:jboss-3.0.4_tomcat-4.1.12 (WINDOWS2000)
WINDOWS2000, linux共用同一套JBoss.

配置JBoss:
启动JBoss需要执行一个脚本文件:
linux:run.sh
WINDOWS对应的是:run.bat

(1)
在JBossin un.bat (for Windows)开头插入一行
set JAVA_HOME = d:s1studio_jdkj2sdk1.4.1
在JBossin un.sh (for Linux)开头插入一行
JAVA_HOME="/root/s1studio_jdk/j2sdk1.4.1"

或者
(2)设置系统环境变量JAVA_HOME,指向JDK

运行JBoss, run.sh或者run.bat
当看到启动JBoss的信息时,说明启动了.
服务器简单的测试:
JBoss默认的WEB端口为8080,我们可以在打开一个浏览器输入地址
http://localhost:8080/jmx-console
当在浏览器看到JBoss的信息时,说明安装配置JBoss成功了.



建立下面的目录和文件(注意大小写).

FIRST.EAR
|
|-----META-INF (application.xml)
|
|-----First.jar
| |-----META-INF (ejb-jar.xml,jboss.xml)
| `-----Dev
| |-----First(FirstSession.java, FirstSessionHome.java, FirstSessionBean.java)
| |-----Delegate(NewDelegate.java)
| `-----Dao(MysqlDao.java)
|
`-----First.war(index.jsp)
|
`-----WEB-INF (jboss-web.xml, web.xml)
|-----classes
`-----lib

/*
**
**MysqlDao.java
**
*/

package Dev.Dao;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import java.sql.ResultSet;import javax.naming.InitialContext;import javax.sql.DataSource;public class MysqlDao { public Connection getConnection() throws Exception { InitialContext ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:/PostgresDS"); Connection conn = null; Statement stmt = null; try { conn = ds.getConnection(); } catch (SQLException sqlEx) { System.out.println("Error connect to pool."); } return conn; } public String getName(String id) throws Exception { Connection conn = null; Statement stmt = null; ResultSet rs = null; String name = ""; try { conn = getConnection(); if ( conn !=null )System.out.println("Get conecttion. "+ conn.toString()); stmt = conn.createStatement(); if ( stmt !=null )System.out.println("Get Statement. "+ stmt.toString()); String sql = "SELECT * from users where id = ´"+id+"´"; System.out.println("Sql from getId(): "+sql); rs = stmt.executeQuery(sql); if ( rs !=null )System.out.println("Get result. "); if (rs.next()){ name = rs.getString("name"); } } catch (Exception sqlEx) { System.out.println("Error from getName()."); System.out.println("Error from DAO.getName() :" + sqlEx.getMessage()); }finally { if (conn != null) { try { conn.close(); } catch (Exception sqlEx) { } } } return name; } public String getCountry(String id) throws Exception { Connection conn = null; Statement stmt = null; String name = ""; try { conn = getConnection(); stmt = conn.createStatement(); String sql = "SELECT * from users where id = ´"+id+"´"; System.out.println("Sql from getCountry(): "+sql); java.sql.ResultSet rs = stmt.executeQuery(sql); if (rs.next()) { name = rs.getString("Country"); } } catch (SQLException sqlEx) { System.out.println("Error from getCountry()."); }finally { if (conn != null) { try { conn.close(); } catch (Exception sqlEx) { } } } return name; }}


/*
**
**NewDelegate.java
**
*/

package Dev.Delegate;import java.lang.*;import Dev.First.*;public class NewDelegate { Dev.First.FirstSession bean = null; public NewDelegate( ){ try { javax.naming.InitialContext ctx = new javax.naming.InitialContext(); Object objref = ctx.lookup("ejb/FirstSession"); Dev.First.FirstSessionHome testBean = (Dev.First.FirstSessionHome) javax.rmi.PortableRemoteObject.narrow (objref,Dev.First.FirstSessionHome.class); bean = testBean.create(); System.out.println("From JSP"); } catch (Exception NamingException) { NamingException.printStackTrace(); } } public String Welcome() { String msg = ""; try { msg = bean.Welcome(); } catch (Exception NamingException) { NamingException.printStackTrace(); } return msg; } public String getName(String id) { String name = ""; try { name = bean.getName(id); } catch (Exception NamingException) { NamingException.printStackTrace();} return name; } public String getCountry(String id) { String country = ""; try { country = bean.getCountry(id); } catch (Exception NamingException) { NamingException.printStackTrace();} return country; } }

/*
**
**FirstSession.java
**
*/

package Dev.First;import java.lang.*;import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.EJBException;import javax.ejb.SessionBean;import javax.ejb.SessionContext;public interface FirstSession extends javax.ejb.EJBObject{ public String Welcome() throws java.rmi.RemoteException; public String getName(String id) throws java.rmi.RemoteException; public String getCountry(String id) throws java.rmi.RemoteException;}


/*
**
**FirstSessionHome.java
**
*/

package Dev.First;import java.lang.*;import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.EJBException;import javax.ejb.SessionBean;import javax.ejb.SessionContext;public interface FirstSessionHome extends javax.ejb.EJBHome{public FirstSession create() throws javax.ejb.CreateException, java.rmi.RemoteException;}


/*
**
**FirstSessionBean.java
**
*/

package Dev.First;import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.EJBException;import javax.ejb.SessionBean;import javax.ejb.SessionContext;public class FirstSessionBean implements SessionBean{public void ejbCreate() throws CreateException {}public String Welcome(){ String msg="Hello! This My Session Bean From Jboss."; System.out.println(msg); return msg;}public String getName(String id){ String name = ""; System.out.println("From bean before getName :"+ name); try{ Dev.Dao.MysqlDao dao = new Dev.Dao.MysqlDao(); name = dao.getName(id); System.out.println("From bean after getName :"+ name); }catch(Exception e){ System.out.println(e.getMessage());} return name;}public String getCountry(String id){ String country = ""; try{ Dev.Dao.MysqlDao dao = new Dev.Dao.MysqlDao(); country = dao.getCountry(id); }catch(Exception e){ } return country;}public void setSessionContext( SessionContext aContext ) throws EJBException {}public void ejbActivate() throws EJBException {}public void ejbPassivate() throws EJBException {}public void ejbRemove() throws EJBException {}}




/*Don´t put the following lines into index.jsp
**
**index.jsp
**
*/Don´t put the above lines into index.jsp


Welcome

Your ID:





Connect to Database OK." %>



<!--不要将此以下5行存入文件
**
**ejb-jar.xml
**
--&gt不要将此以上5行存入文件, 下同.

<?xml version="1.0" encoding="UTF-8"?>FirstFirst<!-- Session Beans --&gtMy First Session BeanFirstSessionDev.First.FirstSessionHomeDev.First.FirstSessionDev.First.FirstSessionBeanStatelessContainer




<!--
**
**jboss.xml
**
--&gt

<?xml version="1.0" encoding="UTF-8"?>FirstSessionejb/FirstSession



<!--
**
**jboss-web.xml
**
--&gt

<?xml version="1.0" encoding="UTF-8"?>jdbc/PostgresDSjavax.sql.DataSourcejava:/PostgresDS



<!--
**
**web.xml
**
--&gt

<?xml version="1.0" encoding="ISO-8859-1"?>Postgresql driverjdbc/PostgresDSjavax.sql.DataSourceContainer




<!--
**
**application.xml
**
--&gt

<?xml version="1.0" encoding="ISO-8859-1"?>FirstFirst.war/FirstFirst.jar



编译JAVA源程序,生成class文件.
进入JAVA源程序目录, 运行:
javac -classpath %classpath%;%jboss%serverdefaultdeployFirst.earFirst.jar *.java
或者
javac -classpath %jboss%serverdefaultdeployFirst.earFirst.jar;%jboss%clientjboss-j2ee.jar *.java

Copy 目录First.ear to jbossserverdefaultdeploy.
打开浏览器输入地址 http://localhost:8080/First

到此, 在浏览器看到: Hello! This My Session Bean From Jboss.
说明这个EJB工作了.

如果按按钮, 没反应或出错. 原因没安装配置数据库, 下面安装配置Postgres数据库


For Windows2000
下载 PgSQL731wina1.exe (http://www.postgresql.org),
Finally you will see the next line, you need enter the password for Administrator
最后你将看下一个行,你必须为用户Administrator输入password.
********************
Enter password of user `.Administrator´:123456
********************

记下此password, 我的口令是123456.

从开始菜单 > Programm > PostgresSQL > Adjust PostgresSQL Configuration file
它将在Wordpad中打开PostgresSQL Configuration文件, 找到下列行,


#
# Connection Parameters
#
#tcpip_socket = false
#ssl = false

#max_connections = 32
#superuser_reserved_connections = 2

#port = 5432

修改编辑:
#
# Connection Parameters
#
tcpip_socket = true
#ssl = false

#max_connections = 32
#superuser_reserved_connections = 2

port = 5432

接着,保存文件.

起动PostgresSQL服务器:
开始菜单>Programm>PostgresSQL>Utilies>Start PostgresSQL server
起动命令行:
开始菜单>Programm>PostgresSQL>Utilies>Command Shell


执行下列命令,准备数据,
Administrator@SAN /
$ dir

$ cd bin

$ createdb test

$ psql test

test=# create table users
test-# (name varchar(20),
test(# id varchar(20),
test(# country varchar(20));
test=# insert into users values (´Sam´, ´123´, ´China´);
test=# insert into users values (´Tom´, ´321´, ´USA´);
test=# insert into users values (´Sean´, ´231´, ´France´);

test=# select * from users;
name | id | country
------+-----+---------
Sam | 123 | China
Tom | 321 | USA
Sean | 231 | France
(3 rows)

test=#

到此, 数据准备就绪.



For RedHat:
以root登陆, 执行下列命令,准备数据,

mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

Open and edit /usr/local/pgsql/data/pg_hba.conf

local all trust
host all 127.0.0.1 255.255.255.255 trust

just delete #, and save.


[root@localhost root]# su - postgres
-bash-2.05b$ /usr/bin/postmaster -i -D /usr/local/pgsql/data >logfile 2>&1 &
-bash-2.05b$ /usr/bin/createdb test
-bash-2.05b$ /usr/local/pgsql/bin/psql test
test=# .......the following same as Windows2000

到此, 数据准备就绪.


执行shutdown.bat or shutdown.sh, 停止Jboss Server.

找到JDBC drive.
为了在Jboss中使用连接池,需要拷贝jdbc drive 到Jboss/server/default/deploy , 在linux 我们能找到/usr/share/pgsql/pgjdbc2.jar,在wondows2000,我们能找到PostgreSQLusrsharepostgresqljavapostgresql.jar
把其中之一复制到Jboss/server/default/deploy


配置Jboss

(1) 复制 $Jboss/examples/jca/postgres-service.xml 到 $Jboss/server/default/deploy/

(2) 打开编辑Jboss/server/default/deploy/postgres-service.xml

PostgresDS


jdbc:postgresql://localhost/test
org.postgresql.Driver
<!--set these only if you want only default logins, not through JAAS --&gt
Administrator
123456


In my example, set Username Administrator, password 123456 for windows 2000
set Username Postgres, no password for Linux.
在我的例子中,
windows2000, 用户:Administrator,password:123456
Linux(RH8.0), 用户:Postgres, 没有password
因为PostgresSQL和windows2000使用不同的default用户名,所以在linux和window2000中这文件不同.当然,你可以加相同的PostgresSQL用户名和password在linux和window2000中, 这样这文件就相同了.

保存文件.

(3) 打开编辑 $Jboss/server/default/conf/standardjbosscmp-jdbc.xml
找到:
java:/DefaultDS
Hypersonic SQL
加入:
java:/PostgresDS
Postgres
保存文件.

(4) open and edit $Jboss/server/default/conf/standardjaws.xml
找到:
java:/DefaultDS
Hypersonic SQL
false
加入:
java:/PostgresDS
Postgres
false
保存文件.

现在重起JBoss.
打开浏览器输入地址 http://localhost:8080/First
输入ID,按按钮.  [@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9650775/viewspace-921925/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9650775/viewspace-921925/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值