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

1/5/2008来源:Java教程人气:6810


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

先安装JSDK,再安装JBoss.
安装JSDK,必须获得一套对应于用户的操作系统的JDK,
我的安装的文件目录是
WINDOWS2000: d:\s1studio_jdk\j2sdk1.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后在$JBoss\client中可找到.
建议安装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)
在JBoss\bin\run.bat (for Windows)开头插入一行
set JAVA_HOME = d:\s1studio_jdk\j2sdk1.4.1
在JBoss\bin\run.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; }}