Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: servlet and sql


Message #1 by "aboamama" <khrtoto@h...> on Sun, 18 Aug 2002 11:55:45
Hi ..


I have two tables in database  student and Enrolment  
I want insert the data into student table and I must have the same  value 
of Student_id  in Enrolment Table 


My problem is SQL statement.

this the code 

	
	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:css";
   ResultSet rs   = null;
		String query;
   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 Student_id, Student_name, Student_address, 
Student_phone,Password,Password2;
			
       Student_id= req.getParameter( "studentid" );
       Student_name = req.getParameter( "name" );
       Student_address = req.getParameter( "Address" );
			 Student_phone = req.getParameter( "phone" );
			 Password = req.getParameter( "password" );
       Password2 = req.getParameter( "password2" );
      

      res.setContentType( "text/html" );
      PrintWriter output = res.getWriter();
	           
      if ( Student_id.equals( "" ) ||
           Student_name.equals( "" ) ||
					 Student_address.equals( "" ) ||
					 Student_phone.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(
         "'" + Student_id + "','" + Student_name + "','" + Student_address 
+ "','" + Student_phone + "','" +Password +
         "','" + Password2 + "'" );
				

      if ( success ) 
         output.print( "<H2>Thank you " + Student_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 student   values (" +
            stringtoinsert + ") SELECT Student_id FROM Enrolment  WHERE 
student.Student_id = Enrolment.Student_id ;" );
         statement.close();
      }
      catch ( Exception e ) {
         System.err.println( 
            "ERROR: Problems with adding new entry" );
         e.printStackTrace();
         System.out.println("insertIntoDB exception! "+e.getMessage());
				 return false;
      }

      return true;
   }

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


  Return to Index