Hi,

I don't think my code is submitting data TWICE.
Here's the code :

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

public class InsertModelInfo3 extends HttpServlet {

 public void doGet( HttpServletRequest req,
HttpServletResponse res)
                        throws ServletException, IOException {

  Connection con = null;
  ResultSet rs = null;
  PreparedStatement ps = null;

  res.setContentType("text/html");
  res.setHeader("Cache-Control","no-cache");

  //return early if this is a HEAD request
  if( req.getMethod().equals("HEAD") ) return;

  PrintWriter out = res.getWriter();

  try {

   // Load and therefore register the Oracle Driver
   DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());

   // Get a connection to the database
   con = DriverManager.getConnection(
                "jdbc:oracle:thin:@:1721:oraDB", "scott", "tiger"
);

   // It's faster when auto commit is off
   con.setAutoCommit (false);


    // Create all the StructDescriptor's needed since
only
    // one StructDescriptor is necessary for each
Oracle object type

    // For Object type INFO_TY
    StructDescriptor info_ty_Desc =
      StructDescriptor.createDescriptor("INFO_TY",
con);

    ps = con.prepareStatement ("insert into model
(model_no, prod) values "
                + "(?,?)" );

    String st = new String();

    // Column 1
    st = req.getParameter("modelno");
    ps.setString (1, st);

    // create a new STRUCT object with model_no for
prod_spec, and link
    // create the embedded object for the prod
    Object [] prod_attributes = new Object [2];

    prod_attributes[0] = req.getParameter("prod");
    prod_attributes[1] =
req.getParameter("prod_link");
    STRUCT new_prod = new STRUCT (info_ty_Desc, con,
prod_attributes);
    ps.setObject (2, new_prod, java.sql.Types.STRUCT);


   if ( ps.executeUpdate () <= 0 ) {
     // was not successful in inserting the values,
since an
     // insert operation should have returned 1, if
successful
     out.println("<html>");
     out.println("<body bgcolor=\"#ffffff\">");
     out.println("<h2>Data Insertion
Unsuccessful...</h2>");
     out.println("<pre>");
   }
   ps.close();

   con.commit();
   con.setAutoCommit( true );

   // Print out the Head of the HTML page
   String title = "Successful insertion of Information
for Model: " +
                        req.getParameter("modelno") ;
   printHead( title, out );

   out.println( "<h2>Information for Model: " +
                        req.getParameter("modelno") +
                " Successfully Saved</h2>" );

   printFoot( out );


  }//end of try
  catch ( SQLException e ) {
    out.println( "---------- SQLException caught
----------" );
    out.println( "Error Message: " + e.getMessage() );
    out.println( "SQLState: " + e.getSQLState() );
    out.println( "Vendor: " + e.getErrorCode() );

    out.println("</pre>");
    out.println("</body>");
    out.println("</html>");
  }
  finally {
    // Always close the database connection.
    try {
      if ( con != null ) {
        con.close();
      }
    }//end of try
    catch (SQLException ignored) { }
  }//end of finally

 }//end of doGet


  public void printHead ( String title, PrintWriter
out )
    throws IOException
  {
    out.println( "<html>" );
    out.println( "<head>" );
    out.println( "<title>" + title + "</title>" );
    out.println( "</head>" );
    out.println( "<body bgcolor=\"#ffffff\">" );

  } // end of printHead


  public void printFoot ( PrintWriter out )
    throws IOException
  {

    out.println( "</body>" );
    out.println( "</head>" );

  } // end of printFoot


 public void doPost( HttpServletRequest req,
HttpServletResponse res)
                        throws ServletException, IOException {

   doGet( req, res );

 }

}//end of class




--- Chris Econn <[EMAIL PROTECTED]> wrote:
Can you provide the source code?

>From: Margaret Fisk <[EMAIL PROTECTED]> | Block

Sounds like your code is submitting the record to the
database twice.
If the
data is already there when you checked, the code found
it there when it
tried to submit, hence the error.

> --- Ravi Shankar Jagarapu <[EMAIL PROTECTED]>
> wrote:
> > Hi,
> >
> > I am trying to save a form ( get method) into the
> > oracle database.
> >
> > I have the first column (varchar) as primary key.
> >
> > Now when I submit the form, I get a html page
> > outputted by the servlet showing the data is
> > commited.
> >
> > When I see the source code of the page, though, I
> > get
> > ---------- SQLException caught ----------
> > Error Message: ORA-00001: unique constraint
> > (SCOTT.MODULES_PK) violated
> >
> > SQLState: 23000
> > Vendor: 1
> >
> > However, this happens, though I give a unique
> value
> > for the primary column.
> >
> > What I don't understand is, why is the source code
> > showing the SQLException?
> >
> > I have queried the database and it shows, the data
> I
> > had submitted through the form.
> >
> > -- Ravi.

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to