Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: Re: error inserting data into database


Message #1 by "Nikolas Sakic" <beelzabeb@c...> on Sun, 11 Aug 2002 11:18:45
Hi,
   Whats your servlet Engine?. It looks like its not the code thats the 
problem, its the  call to the servlet, your servlet is not initializing. 
Can you tell me whats ur servlet engine like, Apache + Jserv, Tomcat, JRUn 
etc....

regards

> hi 

> I have a problem here 

> The  message  I got   is  

> an error occurred. Please try again later .

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

> my code is 

> import java.io.*;
i> mport javax.servlet.*;
i> mport javax.servlet.http.*;
i> mport java.util.*;
i> mport 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