在jboss中如何取得sqlserver的datasource.??

02-12-12 hanker

我用的服务器是: jboss-3.0.4_tomcat-4.1.12

下面是我写的测试代码,无论是直接通过jndi,或者是在ejb当中取,都不能取到,不知道为什幺?

有谁能为我做一下测试,告诉我为什幺?

这问题我挺急的,谢谢了.

下面是测试代码,稍做修改就行的.

package com.mdgen.util;
import java.rmi.RemoteException;
import javax.ejb.RemoveException;
import java.sql.*;
import javax.sql.DataSource;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.mdgen.sql.SqlUtil;
import com.myapp.ejb.OrderQuery.*;

import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
public class TestDatasource {
  public TestDatasource() {
  }
  public  void ConnectDsn() throws NamingException
  {
    Context context = null;
    String jndiName="java:/MSSQLDS";
//    String jndiName="java:/jdbc/MSSQL/we3";
    try{
        Properties props = new Properties();
        props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        props.put("java.naming.provider.url", "jnp://127.0.0.1:1099");
        props.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");

        context = new InitialContext(props);
        DataSource ref =(DataSource) context.lookup(jndiName);
    }catch (NamingException ne){
        System.out.println("ConnectDsn(" + jndiName + "): failure " + ne.getMessage());
    }
 }
 void ConnectEjb(){
    Context context = null;
    String jndi="OrderQueryEJB";
    try{
        Properties props = new Properties();
        props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        props.put("java.naming.provider.url", "jnp://127.0.0.1:1099");
        props.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
        context = new InitialContext(props);
        Object ref = context.lookup(jndi);
        OrderQueryHome orderQueryHome = (OrderQueryHome) PortableRemoteObject.narrow(ref, OrderQueryHome.class);
        OrderQueryRemote orderQuery=orderQueryHome.create();
        System.out.println("ConnectEjb("+jndi+"): success ");
        OrderQueryVO qvo = new OrderQueryVO("orderNumber", "list");
        orderQuery.doOrderQuery(qvo);
        System.out.println("run doOrderQuery success!");
    }
    catch (NamingException ne){
        System.out.println("ConnectEjb("+jndi+"): failure" + ne.getMessage());
    }
    catch(Exception e){

    }
  }
  void ConnectJdbc(){
    Connection conn = null;
    Statement stmt = null;
    ResultSet result = null;
    String query="SELECT * FROM CUSTORD1";
    try{
      conn=SqlUtil.getConnection("com.inet.tds.TdsDriver","jdbc:inetdae:127.0.0.1:1433?database=we3Demo&charset=big5","sa","");
      stmt = conn.createStatement();
      result = stmt.executeQuery(query);
      while(result.next()){
         System.out.println("baseno=["+result.getString("BASENO")+"]");
         break;
      }
    }catch(Exception e){
        e.printStackTrace(System.out);
    }

  }
  public static void main(String[] args) {
    TestDatasource a = new TestDatasource();
    try{
 //     a.ConnectEjb();
      a.ConnectDsn();
    }catch(Exception e){
      System.out.println("run error!");
    }
  }
}

<p>

banq
2002-12-12 11:35

1. 从JNDI获取

InitialContext ic = new InitialContext();

DataSource ds = (DataSource) ic.lookup("java:/sqlserverDS");

2.配置Jboss的java:/sqlserverDS

配置方法你应该能查询到,在deploy下建立sqlserver-service.xml文件

然后在jboss的的conf目录下注册安全机制。

zhengzhiyong
2002-12-12 11:36

最好把jboss的出错消息贴出来

zhengzhiyong
2002-12-12 11:38

BANQ兄抢先了:)

hanker
2002-12-13 10:03

xx各位,解Q了!