Hy,
i have on the server using ORB a class that uses connection with a derby database, but it seems not to connect to database. I query using a client the server with ORB for a string. The result of the string should be another string returned from database.
Bellow is my IDL interface:
module ZodieApp{
interface Zodie{
string zodie(in string a);
};
};
The class used to connect to database is called ZodieDatabase and it uses Derby.
The server app is:
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 ...");
}
}
And the implementation class (the 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;
}
}
I have created the package used in Corba app:
Zodie.class
Zodie.java
ZodieHelper.class
ZodieHelper.java
ZodieHolder.class
ZodieHolder.java
ZodieOperations.class
ZodieOperations.java
ZodiePOA.class
ZodiePOA.java
_ZodieStub.class
_ZodieStub.java
But on the client side I get null pointer exception.
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
That's on the methods, that bring up the string from database.
The ZodieDatabase source code:
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;
}
}
Please help,
Sincerely,
con.close()
is going to throw a null pointer exception is if con is null; do you have sysout output indicating that an error was thrown while trying to connect? It should have further information about what the error is... – arcy