Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: error inserting data into database


Message #1 by "aboamama" <khrtoto@h...> on Wed, 31 Jul 2002 19:06:15
hi 

I have a problem here 

The  message  I got   is  

an error occurred. Please try again later .

............
it seems is not a big problem .....I spent all the day trying to solve 
it ..but there is  no result ...
............

my code is 

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;

public class RegisterStudent extends HttpServlet {
   private Statement statement = null;
   private Connection connection = null;
   private String URL = "jdbc:odbc:Zalina";

   public void init( ServletConfig config )
      throws ServletException
   {
      super.init( config );
   
      try {
         Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
         connection = 
            DriverManager.getConnection( URL, "", "" );
      } 
      catch ( Exception e ) {
         e.printStackTrace();
         connection = null;
      }
   }

   public void doPost( HttpServletRequest req,
                       HttpServletResponse res )
      throws ServletException, IOException
   {
      String Studentid, Name, Password, Password2 ;
   
      Studentid = req.getParameter( "studentid" );
      Name = req.getParameter( "name" );
      Password = req.getParameter( "password" );
      Password2 = req.getParameter( "password2" );
   
      res.setContentType( "text/html" );
      PrintWriter output = res.getWriter();
            
      if ( Studentid.equals( "" ) ||
           Name.equals( "" ) ||
           Password.equals( "" ) ||
      Password2.equals( "" ) ) {
         output.println( "<H3> Please click the back " +
                         "button and fill in all " +
                         "fields.</H3>" );
         output.close();
         return;
      }

      
      boolean success = insertIntoDB(
         "'" + Studentid + "','" + Name + "','" + Password +
         "','" + Password2 + "','" );

      if ( success ) 
         output.print( "<H2>Thank you " + Name +
                       " for registering.</H2>" );
      else
         output.print( "<H2>An error occurred. " +
                       "Please try again later.</H2>" );

      output.close();
   }

   private boolean insertIntoDB( String stringtoinsert )
   {
      try {
         statement = connection.createStatement();
         statement.execute( 
            "INSERT INTO Users values (" +
            stringtoinsert + ");" );
         statement.close();
      }
      catch ( Exception e ) {
         System.err.println( 
            "ERROR: Problems with adding new entry" );
         e.printStackTrace();
         return false;
      }

      return true;
   }

   public void destroy()
   {
      try {
         connection.close();
      }
      catch( Exception e ) {
         System.err.println( "Problem closing the database" );
      }
   }
}

............

can you help me .....

  Return to Index