package edu.gac.karl.gened; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.UnexpectedException; import java.rmi.RMISecurityManager; import java.rmi.server.UnicastRemoteObject; import java.sql.*; public class ServerImpl extends UnicastRemoteObject implements Server { private Connection con; public ServerImpl() throws RemoteException { try{ Class.forName("postgresql.Driver"); con = DriverManager.getConnection ("jdbc:postgresql://kilpinen.mcs.gac.edu/karl-gened", "nobody", "nobody"); } catch(Exception e){ throw new UnexpectedException("trouble setting up JDBC", e); } } public Course[] satisfyAnyAndAvoid(String[] geneds, Conflict[] conflicts) throws RemoteException { java.util.Vector v = new java.util.Vector(); try{ Statement stmt = con.createStatement(); StringBuffer sb = new StringBuffer ("select trim(dptno), trim(crsno), trim(secno), trim(crsdesc)," + " trim(gened), trim(meetday), trim(meethrs)" + " from crsctl, crsscd where crsctl.crscd = crsscd.crscd" + " and maxenrl > 0 and (0=1"); // Note that we used 0=1 above to mean "false" in order to make the // code work both under PostgreSQL (which uses a bool type with the // constant false) and standard SQL92 (which uses the integer 0). for(int i = 0; i < geneds.length; i++){ sb.append(" or gened like '%"); sb.append(geneds[i]); sb.append("%'"); } sb.append(')'); for(int i = 0; i < conflicts.length; i++){ sb.append(" and (meetday not like '%"); sb.append(conflicts[i].getDay()); sb.append("%' or meetday = 'ARR' or meethrs not like '%"); sb.append(conflicts[i].getPeriod()); sb.append("%' or meethrs = 'ARR')"); } ResultSet rs = stmt.executeQuery(sb.toString()); while(rs.next()){ v.addElement(new Course(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7))); } Course[] array = new Course[v.size()]; v.copyInto(array); return array; } catch(Exception e){ throw new UnexpectedException("trouble getting course info", e); } } public static void main(String args[]) { if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } try { ServerImpl obj = new ServerImpl(); Naming.rebind("/karl-gened", obj); } catch (Exception e) { System.out.println("ServerImpl err: " + e.getMessage()); e.printStackTrace(); } } }