Corba应用程序使用数据库

时间:2015-01-27 12:13:54

标签: java corba idl

HY,

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

Bellow是我的IDL界面:

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

用于连接数据库的类称为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 ...");
  }
}

实现类(servant):

 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;


    }




}

请帮忙,

此致

1 个答案:

答案 0 :(得分:0)

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