Re: Servlet Question
"elaineskytta" <[email protected]> Thu, 06 Jan 2005 16:49:26 -0000
| Newsgroups | gmane.comp.java.advanced-servlets |
|---|---|
| Message-ID | <[email protected]> |
THANK YOU! I will look into it right away... Elaine --- In [email protected], Milt Epstein <mepstein@u...> wrote: > On Thu, 6 Jan 2005, elaineskytta wrote: > > > Hi, > > > > I will risk getting chewed out. I have been reading through various > > websites and books and none of them deals with my problem. I realize > > this is "simple" question but I have no one else to ask. > > > > I am running Jakarta Tomcat 5.5.4 on Windows 98. I thought my path was > > set correctly since the stand alone program worked just fine calling > > the MySQLUtilities.java methods and storing to the database but the > > servlet calling the MySQLUtilities.java methods does not. > > > > My Autoexec.bat file has the following lines. > > > > PATH=%PATH%;C:\Java\jdk1.5.0\bin;"C:\Program Files\MySql\Bin" > > set JAVA_HOME=C:\Java\jdk1.5.0 > > set CATALINA_HOME=C:\jakarta-tomcat-5.5.4 > > set > > CLASSPATH=.;C:\jakarta-tomcat-5.5.4\common\lib\servlet-api.jar;C:\jakarta-tomcat-5.5.4\common\lib\jsp-api.jar > > set > > CLASSPATH=%CLASSPATH%;C:\Java\jre1.5.0\lib\ext\mysql-connector-java-3.0.16-ga\mysql-connector-java-3.0.16-ga.jar > > > > So I thought my configuration was ok. > > > > Thanks for being patient with me. > > Tomcat doesn't use CLASSPATH to find things. It's got certain > designated directories where you can put things, depending on certain > factors (e.g. is it a class file or a jar, do you want it available > server wide or to just one application, etc.). You need to figure out > which such directory to put it in (and then put it there, of course). > You should read the Tomcat documentation for more specific information: > > http://jakarta.apache.org/tomcat/tomcat-5.5-doc/ > > In particular, this page may be the relevant one: > > http://jakarta.apache.org/tomcat/tomcat-5.5-doc/class-loader-howto.html > > If the above still doesn't help you figure out the answer, I suggest > you ask on the tomcat-user list. > > > > --- In [email protected], Curtis Cooley > > <curtis.cooley@g...> wrote: > > > You'll get an ass chewing for this being to simple of a question for > > > us servlet gurus, but you need to make sure that your mysql driver jar > > > is in a /lib directory somewhere in your servlet container. For > > > example, depending on the version of Tomcat, you can put it in > > > $TOMCAT_HOME/shared/lib > > > > > > On Wed, 05 Jan 2005 21:41:42 -0000, elaineskytta <elaineskytta@y...> > > wrote: > > > > > > > > > > > > Hi, > > > > > > > > I wrote a servlet that handles MySQL stuff. It works PERFECTLY when I > > > > call it from a MAIN routine. It inserts a record into the MYSQL > > > > database. > > > > > > > > Then when I call it from another servlet, to build a webpage it does > > > > not work. I added some debug to it and it does not find the mysql > > > > driver. The web page comes up with the debug displayed. > > > > > > > > What am I doing wrong? Why does it work from a stand alone program > > > > but not from another servlet? > > > > > > > > I am attaching my main (xTest.java), and my two servlets (Test.java) > > > > and (MySQLUtilities.java) > > > > > > > > Thanks for any light you can shed on the situation. > > > > > > > > Elaine > > > > > > ------------------------------------------------------------------------------------------ > > > > File: xTest.java (WORKS with MySQLUtilities.java) > > > > public class xTest > > > > { > > > > /* This sample program calls the insertUser method and inserts > > > > the record into the database. */ > > > > /* It works correctly. */ > > > > public static void main ( String args[] ) throws Exception > > > > { > > > > > > > > Integer UserId; > > > > String Login; > > > > String Password; > > > > String FirstName; > > > > String LastName; > > > > String eMail; > > > > String Phone; > > > > > > > > UserId = 1; > > > > Login = "xxx"; > > > > Password = "x"; > > > > FirstName = "xxx"; > > > > LastName = "xxx"; > > > > eMail = "xxx@y..."; > > > > Phone = "123-456-7890"; > > > > > > > > String xxx = new > > MySQLUtilities().insertUser(UserId, Login, > > > > Password, FirstName, LastName, eMail, Phone); > > > > > > > > System.out.println (xxx ); > > > > > > > > } > > > > } > > > > > > ---------------------------------------------------------------------------------------------- > > > > File: Test.java (Do NOT work with MySQLUtilities.java) > > > > > > > > import java.io.*; > > > > import java.util.*; > > > > import javax.servlet.*; > > > > import javax.servlet.http.*; > > > > > > > > public class Test extends HttpServlet > > > > { > > > > /* This sample servlet calls the insertUser method. > > InsertUser does > > > > not find the database driver. */ > > > > /* WHY - The exact same code works when called from the > > Main program. */ > > > > public void doGet(HttpServletRequest request, > > > > HttpServletResponse response) > > > > throws IOException, ServletException > > > > { > > > > Integer UserId; > > > > String Login; > > > > String Password; > > > > String FirstName; > > > > String LastName; > > > > String eMail; > > > > String Phone; > > > > > > > > String PageTitle = "Testing MySQL Connection"; > > > > > > > > String PageHeader = "<HTML>\n" + > > > > "<HEAD>\n" + > > > > "<title>" + PageTitle + "</title>\n" + > > > > "</head>\n" + > > > > "<body>\n" + > > > > "<h1>" + PageTitle + "</h1>"; > > > > > > > > String PageFooter = "</body>\n" + > > > > "</html>\n"; > > > > > > > > String strDebug = ""; > > > > > > > > response.setContentType("text/html"); > > > > PrintWriter out = response.getWriter(); > > > > > > > > UserId = 2; > > > > Login = "yyy"; > > > > Password = "y"; > > > > FirstName = "yyy"; > > > > LastName = "yyy"; > > > > eMail = "yyy@y..."; > > > > Phone = "321-321-4321"; > > > > > > > > strDebug = new MySQLUtilities().insertUser(UserId, > > Login, > > > > Password, FirstName, LastName, eMail, Phone); > > > > > > > > out.println(PageHeader + strDebug + PageFooter); > > > > > > > > } > > > > } > > > > ------------------------------------------- > > > > File: MySQLUtilities.java > > > > > > > > import java.io.*; > > > > import java.util.*; > > > > import javax.servlet.*; > > > > import javax.servlet.http.*; > > > > import java.sql.*; > > > > > > > > /* This code works correctly when called from Main. The mysql driver > > > > is found and > > > > the data is inserted into the database. When called from the "Test" > > > > servlet the > > > > mysql driver is not found. WHY? */ > > > > public class MySQLUtilities extends HttpServlet > > > > { > > > > private static String strDebug = ""; > > > > > > > > private static String host = "localhost"; > > > > private static String database = "elearning"; > > > > > > > > > > /*********************************************************************** > > > > * > > > > * Opens the connection to the MySQL database > > > > * > > > > > > > > > > ***********************************************************************/ > > > > protected Connection openConnection ( String host, String > > database ) > > > > throws Exception > > > > { > > > > > > > > try > > > > { > > > > strDebug = strDebug + " Before > > Class.forName "; > > > > Class.forName ( "org.gjt.mm.mysql.Driver" ); > > > > strDebug = strDebug + " After Class.forName "; > > > > > > > > System.out.println ( "MySQL Driver Found" ); > > > > } > > > > catch ( java.lang.ClassNotFoundException e ) > > > > { > > > > System.out.println("MySQL JDBC Driver not > > found ... "); > > > > strDebug = strDebug + " MySQL JDBC Driver > > not found ... "; > > > > > > > > throw ( e ); > > > > } > > > > > > > > String url = ""; > > > > try > > > > { > > > > url = "jdbc:mysql://" + host + "/" + database; > > > > > > > > Connection con = DriverManager.getConnection(url); > > > > System.out.println("Connection established > > to " + url + "..."); > > > > > > > > return con; > > > > } > > > > catch ( java.sql.SQLException e ) > > > > { > > > > System.out.println("Connection couldn't be > > established to " + url); > > > > throw ( e ); > > > > } > > > > } > > > > > > > > > > /*********************************************************************** > > > > * > > > > * Closes the connection to the MySQL database > > > > * > > > > > > > > > > ***********************************************************************/ > > > > protected void closeConnection (Connection con) > > > > throws Exception > > > > { > > > > try > > > > { > > > > /* Close the connection */ > > > > con.close(); > > > > } > > > > catch ( SQLException e ) > > > > { > > > > System.out.println("Exception in > > closeConnection"); > > > > throw(e); > > > > } > > > > > > > > } > > > > > > > > > > /*********************************************************************** > > > > * This method executes an SQL statement > > > > * @param con - database connection > > > > * @param sqlStatement - to be executed (create, insert, > > > > update, drop) > > > > > > > > > > ***********************************************************************/ > > > > protected void executeSQL ( Connection con, String > > sqlStatement ) > > > > throws Exception > > > > { > > > > try > > > > { > > > > Statement s = con.createStatement ( ); > > > > s.execute ( sqlStatement ); > > > > s.close ( ); > > > > } > > > > catch ( SQLException e ) > > > > { > > > > System.out.println ( "Error executing sql > > statement" ); > > > > throw ( e ); > > > > } > > > > } > > > > > > > > > > /*********************************************************************** > > > > This function will insert one record into the User table > > > > > > > > > > ***********************************************************************/ > > > > public String insertUser (Integer UserId, String Login, String > > > > Password, > > > > String FirstName, String LastName, String eMail, > > String Phone) > > > > { > > > > > > > > try > > > > { > > > > String sqlStr; > > > > sqlStr = "insert into User (UserId, Login, > > Password, FirstName, > > > > LastName, eMail, Phone) values ("; > > > > sqlStr = sqlStr + UserId.toString() + ", > > '" + Login + "', '" + > > > > Password + "', '" + FirstName + "', '" + > > > > LastName + "', '" + eMail + "', '" > > + Phone + "')"; > > > > > > > > strDebug = strDebug + "Before openConnection "; > > > > > > > > /* Open connection to the database */ > > > > Connection con = openConnection (host, > > database); > > > > > > > > strDebug = strDebug + "After openConnection "; > > > > > > > > /* Insert the User record into the > > database. */ > > > > executeSQL (con, sqlStr); > > > > > > > > /* Close the database */ > > > > closeConnection (con ); > > > > } > > > > catch ( Exception e ) > > > > { > > > > System.out.println("Exception in Insert > > User\n"); > > > > } > > > > > > > > return(strDebug); > > > > } > > > > } > > > > > > > > Before posting a question, try to find your answer here: > > > > <http://www.egroups.com/links/advanced-servlets> > > > > Announcements should go to: [email protected] > > > > To Post a message, send it to: advanced-servlets@e... > > > > To Unsubscribe, send a blank message to: > > advanced-servlets-unsubscribe@e... > > > > Yahoo! Groups Links > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > Curtis Cooley > > > curtis.cooley@g... > > > > > > > > > > > > Before posting a question, try to find your answer here: > > <http://www.egroups.com/links/advanced-servlets> > > Announcements should go to: [email protected] > > To Post a message, send it to: advanced-servlets@e... > > To Unsubscribe, send a blank message to: advanced-servlets-unsubscribe@e... > > Yahoo! Groups Links > > > > > > > > > > > > > > > > Milt Epstein > Research Programmer > Integration and Software Engineering (ISE) > Campus Information Technologies and Educational Services (CITES) > University of Illinois at Urbana-Champaign (UIUC) > mepstein@u... Before posting a question, try to find your answer here: <http://www.egroups.com/links/advanced-servlets> Announcements should go to: [email protected] To Post a message, send it to: [email protected] To Unsubscribe, send a blank message to: [email protected] Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/advanced-servlets/ <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/