使用数据库的 Corba 应用程序

Corba application using database

嗨,

我在使用 ORB 的服务器上有一个 class,它使用与 derby 数据库的连接,但它似乎没有连接到数据库。我使用客户端查询带有 ORB 的服务器的字符串。字符串的结果应该是从数据库返回的另一个字符串。

下面是我的 IDL 接口:

    module ZodieApp{
   interface Zodie{
      string zodie(in string a);
   };
};

用于连接数据库的 class 称为 ZodieDatabase,它使用 Derby。

服务器应用程序是:

    import ZodieApp.*;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

public class ZodieServer {
  public static void main(String args[]) {
    try{

      ORB orb = ORB.init(args,null);


      POA rootPOA=POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
      rootPOA.the_POAManager().activate();


      ZodieImpl zodieImpl = new ZodieImpl(orb);


      org.omg.CORBA.Object ref=rootPOA.servant_to_reference(zodieImpl);
      Zodie href = ZodieHelper.narrow(ref);


      org.omg.CORBA.Object objRef =
          orb.resolve_initial_references("NameService");
      NamingContextExt ncRef=NamingContextExtHelper.narrow(objRef);


      String name = "ZodieService";
      NameComponent path[] = ncRef.to_name(name);
      ncRef.rebind(path,href);

      System.out.println("ZodieServer ready and waiting ...");


      orb.run();
    } 
    catch (Exception e) {
      System.err.println("ERROR: " + e.getMessage());
    }
    System.out.println("ZodieServer Exiting ...");
  }
}

并执行class(仆人):

 import ZodieApp.*;
import org.omg.CORBA.ORB;

public class ZodieImpl extends ZodiePOA {
  private ORB orb;

  public ZodieImpl(ORB orb) {
    this.orb = orb; 
  }

  public String zodie(String a){
   String zodieC = ZodieDatabase.getZodie(a);
   return zodieC;
  }
}

我已经创建了在 Corba 应用程序中使用的包:

      Zodie.class
  Zodie.java
  ZodieHelper.class
  ZodieHelper.java
  ZodieHolder.class
  ZodieHolder.java
  ZodieOperations.class
  ZodieOperations.java
  ZodiePOA.class
  ZodiePOA.java
  _ZodieStub.class
  _ZodieStub.java

但是在客户端我得到了空指针异常。

Caused by: java.lang.NullPointerException
    at ZodieDatabase.getZodie(ZodieDatabase.java:66)
    at ZodieImpl.zodie(ZodieImpl.java:12)
    at ZodieApp.ZodiePOA._invoke(ZodiePOA.java:38)
    at com.sun.corba.se.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(Unknown Source)
    ... 10 more

这是从数据库中调出字符串的方法。

ZodieDatabase 源代码:

import ZodieApp.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ZodieDatabase{  
      private String a="";
      private String zodie="aloha";
      static Statement instructiune=null;
      static Connection con=null;
      static String jdbcDriver="org.apache.derby.jdbc.ClientDriver";
      static String URLBazaDate="jdbc:derby://localhost:1527/AN_CHINEZESC";

public static void main(String[] args){
          System.out.println("Zodia:"+getZodie("1931-01-01"));
      }



        public static String getZ(String an) {
            return "Some value here";
        }

      public static String getZodie(String an){

          String zodie=null;

          try{


          Class.forName(jdbcDriver).newInstance();
          con=DriverManager.getConnection(URLBazaDate);
          instructiune=con.createStatement();
          try{
              String sql = "select an from inceput where date(an||\'-\'||luna||\'-\'||zi)="+
                      "(select max(date(an||\'-\'||luna||\'-\'||zi)) from inceput t where date(an||\'-\'||luna||\'-\'||zi) <= date(\'"
                      +an+"\'))";
              ResultSet rs=instructiune.executeQuery(sql);
              while(rs.next()){
                    //out.print(rs.getString("an"));
                 an = rs.getString("an");
                  zodie = ZodiacChinezesc.gasesteZodia(an);

                  }
          }
          catch(SQLException ex){

          }


        }
        catch(ClassNotFoundException ex){
          System.out.println("Driver inexistent JDBC: "+jdbcDriver);
        }
        catch(SQLException ex){
          System.out.println("Baza de date inexistenta "+URLBazaDate);
        }
        catch(Exception ex){
          System.out.println("Eroare : "+ex.getMessage());
        }    

          finally {
              try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }



      return zodie;


    }




}

请帮忙,

此致,

我解决了上面的问题。我将数据库 class 移动到实现 class 中,之后我从命令提示符编译以包含 derby jar。