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

hanker發表於2002-12-12

我用的伺服器是: 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 class="indent">

相關文章