Foros del Web » Programación para mayores de 30 ;) » Java »

insertar registros con resultados en JSP

Estas en el tema de insertar registros con resultados en JSP en el foro de Java en Foros del Web. hola a todos necesito de su valiosisima ayuda Necesito insertar 4 valores a una tabla llamada NUMEROS, esta tabla contiene 4 columnas (NUM1, NUM2, NUM3, ...
  #1 (permalink)  
Antiguo 19/05/2004, 00:09
Avatar de cookie  
Fecha de Ingreso: agosto-2002
Mensajes: 688
Antigüedad: 22 años, 4 meses
Puntos: 0
insertar registros con resultados en JSP

hola a todos

necesito de su valiosisima ayuda

Necesito insertar 4 valores a una tabla llamada NUMEROS, esta tabla contiene 4 columnas (NUM1, NUM2, NUM3, NUM4,NUM5, RESULT)

Lo que quiero hacer es insertar por medio de un formulario los 5 valores por medio de TEXTBOX (hasta aqui no hay problema), donde comienza mi bronca es que quiero que automaticamente me inserte el resultado de la suma de estos 5 numeros en el campo RESULT

¿esto es posible?

Si me pudieran ayudar a ver como se hace en JSP


O ALGUNA OTRA SUGERENCIA

Intente mostrar los resultados asi:

SELECT (NUMERO1+NUMERO2+NUMERO3) AS result
FROM NUMBERS

y si muestra los resultados, pero no los guarda en la BD.

gracias

ahi va mi codigo

<%@ 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 = "IDagent|value|PS|value|CM|value|EX|value|CS|value |BL|value|resultado|value|Retailer|value";
String MM_columnsStr = "IDagent|none,none,NULL|PS|none,none,NULL|CM|none, none,NULL|EX|none,none,NULL|CS|none,none,NULL|BL|n one,none,NULL|resultado|none,none,NULL|Retailer|', none,''";

// 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">IDagent:</td>
<td>
<input type="text" name="IDagent" value="" size="32">
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">num1:</td>
<td>
<input type="text" name="PS" value="" size="32">
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">num2:</td>
<td>
<input type="text" name="CM" value="" size="32">
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">num3:</td>
<td>
<input type="text" name="EX" value="" size="32">
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">num4:</td>
<td>
<input type="text" name="CS" value="" size="32">
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">num5:</td>
<td>
<input type="text" name="BL" value="" size="32">
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Resultado:</td>
<td>
<input name="resultado" type="text" size="32">
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Retailer:</td>
<td>
<select name="Retailer">
</select>
</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>
__________________
___________________________

do the evolution ¡
Mineriaenlinea.com
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:52.