[转]整合JBoss 3.x 和MS SQL Server 2000
2006-08-25 23:07
447 查看
准备工作[/b]
安装JBoss 3.x和MS SQL Server 2000
下载Microsoft SQL Server 2000 Driver for JDBC
(http://www.microsoft.com/downloads/details.aspx?familyid=07287b11-0502-461a-b138-2aa54bfdc03a&displaylang=en#filelist)
安装MS SQL Server 2000的sp3补丁包( http://www.microsoft.com/downloads/details.aspx?FamilyId=90DCD52C-0488-4E46-AFBF-ACACE5369FA3&displaylang=zh-cn#filelist )。注意,不升级的话是无法正确连接到MS SQL Server 2000上的,会出现Error establishing socket错误。
开始整合[/b]
1. 安装好Microsoft SQL Server 2000 Driver for JDBC后,在其安装目录\lib 下有三个包:msbase.jar、mssqlserver.jar、msutil.jar。把他们拷贝到%JBoss安装目录%\server\default\lib下
2. 将Jboss目录下的docs\examples\jca\中的mssql-ds.xml和mssql-xa-ds.xml拷贝到\server\default\deploy\目录下,并做相应修改(红色部分):
mssql-ds.xml[/b]:[/b]
<datasources>
<local-tx-datasource>
<jndi-name>MSSQLDS</jndi-name>
<connection-url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind</connection-url>
<driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
<user-name>sa</user-name>
<password></password>
</local-tx-datasource>
</datasources>
mssql-xa-ds.xml:[/b]
<datasources>
<xa-datasource>
<jndi-name>MSSQLXADS</jndi-name>
<xa-datasource-class>com.microsoft.jdbcx.sqlserver.SQLServerDataSource</xa-datasource-class>
<xa-datasource-property name="ServerName">localhost</xa-datasource-property>
<xa-datasource-property name="DatabaseName">Northwind</xa-datasource-property>
<xa-datasource-property name="SelectMethod">cursor</xa-datasource-property>
<!-- not sure if these should be here-->
<user-name>sa</user-name>
<password/>
</xa-datasource>
</datasources>
整合测试
[/b]
1. 编写SessionBean,在其中添加如下方法:
public String testDS() {
String resl = null;
try {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context ctx = new InitialContext(p);
resl += "testing the database...\n";
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:/MSSQLDS");
//这个”MSSQLDS”必须和mssql-ds.xml[/b]文件中的jndi-name保持一致,否则会出错
java.sql.Connection conn =null;
java.sql.Statement st=null;
java.sql.ResultSet rs=null;
try {
conn = ds.getConnection();
st = conn.createStatement();
String sqlStr = "select * from Employees";
rs = st.executeQuery(sqlStr);
while (rs.next()) {
resl += rs.getString("FirstName") + " " +rs.getString("LastName")+"\n";
}
} catch (Exception e) {
}
finally {
if(rs!=null) rs.close();
if(st!=null) st.close();
if(conn!=null) conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return resl;
}
2. 部署EJB
3. 测试SessionBean
public class test{
public void run() {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
try {
Context ctx = new InitialContext(p);
Object obj = ctx.lookup("testBean");
testHome home = (testHome) PortableRemoteObject.narrow(obj, testHome.class);
test t = home.create();
System.out.println("test...");
String str = t.testDS();
System.out.println("received: \n" + str);
System.out.println("test finished!\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new test().run();
}
}
安装JBoss 3.x和MS SQL Server 2000
下载Microsoft SQL Server 2000 Driver for JDBC
(http://www.microsoft.com/downloads/details.aspx?familyid=07287b11-0502-461a-b138-2aa54bfdc03a&displaylang=en#filelist)
安装MS SQL Server 2000的sp3补丁包( http://www.microsoft.com/downloads/details.aspx?FamilyId=90DCD52C-0488-4E46-AFBF-ACACE5369FA3&displaylang=zh-cn#filelist )。注意,不升级的话是无法正确连接到MS SQL Server 2000上的,会出现Error establishing socket错误。
开始整合[/b]
1. 安装好Microsoft SQL Server 2000 Driver for JDBC后,在其安装目录\lib 下有三个包:msbase.jar、mssqlserver.jar、msutil.jar。把他们拷贝到%JBoss安装目录%\server\default\lib下
2. 将Jboss目录下的docs\examples\jca\中的mssql-ds.xml和mssql-xa-ds.xml拷贝到\server\default\deploy\目录下,并做相应修改(红色部分):
mssql-ds.xml[/b]:[/b]
<datasources>
<local-tx-datasource>
<jndi-name>MSSQLDS</jndi-name>
<connection-url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind</connection-url>
<driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
<user-name>sa</user-name>
<password></password>
</local-tx-datasource>
</datasources>
mssql-xa-ds.xml:[/b]
<datasources>
<xa-datasource>
<jndi-name>MSSQLXADS</jndi-name>
<xa-datasource-class>com.microsoft.jdbcx.sqlserver.SQLServerDataSource</xa-datasource-class>
<xa-datasource-property name="ServerName">localhost</xa-datasource-property>
<xa-datasource-property name="DatabaseName">Northwind</xa-datasource-property>
<xa-datasource-property name="SelectMethod">cursor</xa-datasource-property>
<!-- not sure if these should be here-->
<user-name>sa</user-name>
<password/>
</xa-datasource>
</datasources>
整合测试
[/b]
1. 编写SessionBean,在其中添加如下方法:
public String testDS() {
String resl = null;
try {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context ctx = new InitialContext(p);
resl += "testing the database...\n";
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:/MSSQLDS");
//这个”MSSQLDS”必须和mssql-ds.xml[/b]文件中的jndi-name保持一致,否则会出错
java.sql.Connection conn =null;
java.sql.Statement st=null;
java.sql.ResultSet rs=null;
try {
conn = ds.getConnection();
st = conn.createStatement();
String sqlStr = "select * from Employees";
rs = st.executeQuery(sqlStr);
while (rs.next()) {
resl += rs.getString("FirstName") + " " +rs.getString("LastName")+"\n";
}
} catch (Exception e) {
}
finally {
if(rs!=null) rs.close();
if(st!=null) st.close();
if(conn!=null) conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return resl;
}
2. 部署EJB
3. 测试SessionBean
public class test{
public void run() {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
try {
Context ctx = new InitialContext(p);
Object obj = ctx.lookup("testBean");
testHome home = (testHome) PortableRemoteObject.narrow(obj, testHome.class);
test t = home.create();
System.out.println("test...");
String str = t.testDS();
System.out.println("received: \n" + str);
System.out.println("test finished!\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new test().run();
}
}
相关文章推荐
- [转]整合JBuilder2005和MS SQL Server 2000
- MS SQL Server 2000安装不成功的原因 .
- 转摘--MS SQL Server 2000 数据库使用备份还原造成的孤立用户和对象名‘xxx’无效的错误的解决办法
- MS SQL Server 2000 数据库使用备份还原造成dbo登录名丢失解决法
- delphi中对MS SQL Server 2000的操作组件
- MS SQL Server 2000版在windows server 2003企业版系统上运行时造成数据库suspect的解决方法
- MS SQL SERVER 2000 服务器别名保存在注册表中
- 连接远程ms sql server 2000企业版时出现错误:10061的解决方法
- MS SQL Server 2000 的版本
- MS SQL Server 2000安装不成功的原因
- 《MS SQL Server 2000管理员手册》系列——23. 从因特网存取 Microsoft SQL Server
- 《MS SQL Server 2000管理员手册》系列——15. 使用Transact-SQL和Enterprise Manager管理数据表
- 《MS SQL Server 2000管理员手册》系列——目录
- 如何在rails中正确使用MS Sql Server 2000做为数据库服务器(zz)
- MS SqlServer 2000交换排序的一种方法
- 将MS SQLServer 2005导入 MS SQLServer 2000的方法及注意事项
- 使用MyEclipse7.5连接MS SQL Server 2000
- MS SQL Server 2000安装不成功的原因
- 将 MS SQL Server 2005 SP2 整合到安装文件--【脚本修正】
- 如何更改MS SQL Server 2000/MSDE2000的端口号?