dao技术(一)
使用JBoss和PostgreSQL-----快速开发EJB和J2EE Web Application
hanqw3 原创 (参与分:20,专家分:30) 发表:2003-8-4 上午11:46 更新:2003-8-4 下午2:49 版本:2.0 阅读:4218次
使用JBoss和PostgreSQL-----快速开发EJB和J2EE Web Application

Developing J2EE Web Application on Jboss and PostgreSQL(chinese version)


作者:Han QW, 转载请指明出处 如有不当之处,敬请指出


先安装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;
}

}


/*
**
**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.

hofman   2005-11-19 22:34:38 评论:0   阅读:1720   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog