Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
| Search | Today's Posts | Mark Forums Read
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2004, 10:07 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with insert

HI everybody

I have a problem, i need insert into a DB 6 values trough a FORM,

i need to insert manually 5 values (which are called PS, CM, CS, EX, BL) ...**the problem is ----> and the sixth value must be the result of adding PS+CM+CS+EX+BL

i have the next code, but i dont have any idea of how perform this calculation on "inserting" time

Can you help me ..please ¡

__________________________________________________ __
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="Connections/myconexion.jsp" %>
<%
// *** Edit Operations: declare variables

// set the form action variable
String MM_editAction = request.getRequestURI();
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
  String queryString = request.getQueryString();
  String tempStr = "";
  for (int i=0; i < queryString.length(); i++) {
    if (queryString.charAt(i) == '<') tempStr = tempStr + "&lt;";
    else if (queryString.charAt(i) == '>') tempStr = tempStr + "&gt;";
    else if (queryString.charAt(i) == '"') tempStr = tempStr + "&quot;";
    else tempStr = tempStr + queryString.charAt(i);
  }
  MM_editAction += "?" + tempStr;
}

// connection information
String MM_editDriver = null, MM_editConnection = null, MM_editUserName = null, MM_editPassword = null;

// redirect information
String MM_editRedirectUrl = null;

// query string to execute
StringBuffer MM_editQuery = null;

// boolean to abort record edit
boolean MM_abortEdit = false;

// table information
String MM_editTable = null, MM_editColumn = null, MM_recordId = null;

// form field information
String[] MM_fields = null, MM_columns = null;
%>
<%
// *** Insert Record: set variables

if (request.getParameter("MM_insert") != null && request.getParameter("MM_insert").toString().equal s("form1")) {

  MM_editDriver = MM_myconexion_DRIVER;
  MM_editConnection = MM_myconexion_STRING;
  MM_editUserName = MM_myconexion_USERNAME;
  MM_editPassword = MM_myconexion_PASSWORD;
  MM_editTable = "DB.agents";
  MM_editRedirectUrl = "";
  String MM_fieldsStr = "PS|value|CM|value|EX|value|CS|value|BL|value|resu ltado|value";
  String MM_columnsStr = "PS|none,none,NULL|CM|none,none,NULL|EX|none,none, NULL|CS|none,none,NULL|BL|none,none,NULL|resultado |none,none,NULL";

  // create the MM_fields and MM_columns arrays
  java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_fieldsStr,"|");
  MM_fields = new String[tokens.countTokens()];
  for (int i=0; tokens.hasMoreTokens(); i++) MM_fields[i] = tokens.nextToken();

  tokens = new java.util.StringTokenizer(MM_columnsStr,"|");
  MM_columns = new String[tokens.countTokens()];
  for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] = tokens.nextToken();

  // set the form values
  for (int i=0; i+1 < MM_fields.length; i+=2) {
    MM_fields[i+1] = ((request.getParameter(MM_fields[i])!=null)?(String)request.getParameter(MM_fields[i]):"");
  }

  // append the query string to the redirect URL
  if (MM_editRedirectUrl.length() != 0 && request.getQueryString() != null) {
    MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + request.getQueryString();
  }
}
%>
<%
// *** Insert Record: construct a sql insert statement and execute it

if (request.getParameter("MM_insert") != null) {

  // create the insert sql statement
  StringBuffer MM_tableValues = new StringBuffer(), MM_dbValues = new StringBuffer();
  for (int i=0; i+1 < MM_fields.length; i+=2) {
    String formVal = MM_fields[i+1];
    String elem;
    java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_columns[i+1],",");
    String delim = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    String altVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    String emptyVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";
    if (formVal.length() == 0) {
      formVal = emptyVal;
    } else {
      if (altVal.length() != 0) {
        formVal = altVal;
      } else if (delim.compareTo("'") == 0) { // escape quotes
        StringBuffer escQuotes = new StringBuffer(formVal);
        for (int j=0; j < escQuotes.length(); j++)
          if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\'');
        formVal = "'" + escQuotes + "'";
      } else {
        formVal = delim + formVal + delim;
      }
    }
    MM_tableValues.append((i!=0)?",":"").append(MM_col umns[i]);
    MM_dbValues.append((i!=0)?",":"").append(formVal);
  }
  MM_editQuery = new StringBuffer("insert into " + MM_editTable);
  MM_editQuery.append(" (").append(MM_tableValues.toString()).append(") values (");
  MM_editQuery.append(MM_dbValues.toString()).append (")");

  if (!MM_abortEdit) {
    // finish the sql and execute it
    Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance() ;
    Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_e ditUserName,MM_editPassword);
    PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toStri ng());
    MM_editStatement.executeUpdate();
    MM_connection.close();

    // redirect with URL parameters
    if (MM_editRedirectUrl.length() != 0) {
      response.sendRedirect(response.encodeRedirectURL(M M_editRedirectUrl));
      return;
    }
  }
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form method="post" action="<%=MM_editAction%>" name="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">PS:</td>
      <td>
        <input type="text" name="PS" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">CM:</td>
      <td>
        <input type="text" name="CM" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">EX:</td>
      <td>
        <input type="text" name="EX" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">CS:</td>
      <td>
        <input type="text" name="CS" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">BL:</td>
      <td>
        <input type="text" name="BL" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Resultado:</td>
      <td>
        <input type="text" name="resultado" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td>
        <input type="submit" value="Insert record">
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>
__________________________________________________ _____________


:) thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
insert problem lscjtw XSLT 2 August 3rd, 2007 10:29 AM
insert into problem yami56 Access 3 March 3rd, 2005 05:16 AM
Validation and Insert problem addos Beginning PHP 0 January 17th, 2005 06:50 PM
Insert problem CW Classic ASP Databases 14 September 9th, 2003 04:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.