/* 3/1/2000 */ /* ***************************************************************************** Copyright 2000 by Erica Andrews (cylikon@hotmail.com). All rights reserved. ./ButchWhipAppeal/ ./IcedPinky/ NO portion of this software may be used in ANY way by anyone other than myself without my express written consent. If it is downloaded for testing, it must be deleted from your system within 24 hours. Any other use constitutes piracy and fraudulent misuse of this software, punishable in a court of law. Copyright 2000 by Erica Andrews. All rights reserved. ***************************************************************************** */ import CorbaSearchEng.*; import CorbaSearchEng.CorbaSearch; import java.sql.*; import java.net.*; import java.util.*; import java.io.*; import org.omg.CosNaming.*; // not used import org.omg.CosNaming.NamingContextPackage.*; // not used import org.omg.CORBA.*; public class SearchServant extends CorbaSearchEng._CorbaSearchImplBase { private int rowcount; private int columncount; Vector v = new Vector(); private String sword; // This is the string that will be returned if the query is empty (no results). // The value of the string will only change if the query has at least one row. public String resultset = "Not Ready Yet!"; private com.visigenic.vbroker.orb.ORB _orb; //not really using super in this example /* We could go ahead and establish a connection to the database and load the JDBC driver. However, I do not like the idea of leaving database connections open which may not be in use continuously. Instead, I'll just load the driver at server start-up... */ public SearchServant(com.visigenic.vbroker.orb.ORB orb, String objname) { super(objname); _orb = orb; System.out.println(" Loading JDBC Driver gwe.sql.gweMysqlDriver ... "); try { Class.forName("gwe.sql.gweMysqlDriver"); } catch (ClassNotFoundException nc) { System.out.print("Error loading JDBC driver!: "+nc+nc.getMessage()); } System.out.println(" Loading complete."); } // the actual methods from IDL being implemented ********* public void setSearchword(String searchword) { sword = searchword; this.sword = sword; } public String doQuery() throws CorbaSearchEng.QueryError { try { System.out.print("\n The Searchword was: "+sword); System.out.print("\n Connecting to database... "); Connection conn = DriverManager.getConnection ("jdbc:mysql://:3306/siteindex2","root","6ummdbo"); Statement stmt = conn.createStatement (); ResultSet rs = stmt.executeQuery ("SELECT DISTINCT thesites.itsgroup,thesites.title,thesites.url,thesites.thedate FROM thesites,keywords WHERE keyword LIKE '%"+sword+"%' AND thesites.url=keywords.url"); int rowcount = 0; // for counting the rows in the ResultSet // First, lets count the number of results / rows while(rs.next()) { rowcount++; // How many rows do we have?? String breaker = "NEW_ROW"; // now lets actually start putting the db columns into the arrays for (int x = 1; x <= rs.getMetaData().getColumnCount(); x++) { v.addElement(rs.getString(x).trim()); } //end of for loop // now lets all a special string to the Vector to indicate a new row v.addElement(breaker); } //end of while this.v = v; this.rowcount = rowcount; // set the rowcount int value for the whole class // Now, let's count the number of columns columncount = rs.getMetaData().getColumnCount(); this.columncount = columncount; System.out.print("\n Rows: "+rowcount+". Columns: "+columncount+".\n"); // If the query was did not return 0 rows, let's change the value of the resultset String if (rowcount != 0) { String tempstring = v.toString(); // converting the vector to a string // Right now, the String looks something like [string1,string2,string3] // so let's remove the brackets [ ] - the commas will be removed later by the client // through a StringTokenizer. (The commas need to be saved to keep columns and rows // seperate. The brackets will be replaced with empty characters (spaces), then // the trim() method will be called to removed extra white space. int b1 = tempstring.indexOf("["); int b2 = tempstring.indexOf("]"); String emptystring = " "; if (b1 != -1) { // the indexOf int will equal -1 if there is no ] found in the string tempstring = tempstring.replace(tempstring.charAt(b1),emptystring.charAt(1)); } if (b2 != -1) { tempstring = tempstring.replace(tempstring.charAt(b2),emptystring.charAt(1)); } tempstring = tempstring.trim(); setResultset(tempstring); // set the value of the resultset string for the whole class // closing all database resources rs.close(); stmt.close(); conn.close(); System.out.println(" Connection to database closed."); } // end if else { setResultset("No Results Found For Your Query."); } //end else // clearing the Vector to conserve system resources and to make sure no old data lingers v.removeAllElements(); } catch (Throwable e){ throw new CorbaSearchEng.QueryError("ERROR: "+e+" "+e.getMessage());} // finally, lets return our values... return getResultset(); } //end of DoQuery method // rowcount() method public int rowcount() { return rowcount; } // columncount() method public int columncount() { return columncount; } public void setResultset(String str) { resultset = str; this.resultset = resultset; } public String getResultset() { return resultset; } } //end of class