Re: Servlet Question

Curtis Cooley <[email protected]> Wed, 5 Jan 2005 13:50:46 -0800
Newsgroups gmane.comp.java.advanced-servlets
Message-ID <[email protected]>
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 <[email protected]> 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 = "[email protected]";
>                 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 = "[email protected]";
>                 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: [email protected]
> To Unsubscribe, send a blank message to: [email protected]
> Yahoo! Groups Links
> 
> 
> 
> 
> 


-- 
Curtis Cooley
[email protected]


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/