Its very simple to write a SQL after getting all the form elements values in some 
variables. You just need to concatenate all the values and need to create a INSERT 
statement. See for example  :-

String var1 = request.getParameter("var1);
String var2 = request.getParameter("var2);
String var3 = request.getParameter("var3);

String query = "INSERT INTO TABLENAME(VAR1, VAR2, VAR3) VALUES(";
if (var1 != null && var1.trim().length() != 0)
        query += "'" + var1  "',";
if (var2 != null && var2.trim().length() != 0)
        query += "'" + var2  "',";
if (var3 != null && var3.trim().length() != 0)
        query += "'" + var3  "',";

if (query.charAt(query.length() - 1) == ',') 
        query = query.substring(0, query.length()); 

Now your whole query is ready in query variable.

If still have any doubt then you can ask me.

This is just an approach. You can take care of all kinds of types here like int, date, 
etc and instead of String for query use StringBuffer which is much more efficient.

-----Original Message-----
From: Graeme McLaren [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, April 16, 2002 4:20 AM
To: [EMAIL PROTECTED]
Subject: JSPs/DBs and HTML Forms


Hi everyone, I'm completely new to JSPs and I'm finding a somewhat daunting
area.  Specifically I'm having a problem inserting information from an html
form to a database using a JSP.

Basically I've got the problem of getting the parameter and the value from
the form then inserting it in to the database with SQL.

I can use  mystring=request.getParameter("TheNameOfTheFormInput");  to get
the value from the form elements.  Once I've done this I'm completely lost
as to how to write the SQL to insert the values from the form elements INTO
the database.

Can anyone guide me through this, point me in the right direction or give me
any tips at all.  Any advice would be greatly appreciated.

Thank you in advance,

Graeme :)

P.S. Currently I've got a JSP like this:

<!-- imports the JDBC functions -->
<%@ page import="java.sql.*" %>
<html>
<head>
<title>JDBC Example</title>
</head>
<body>
<h1>JDBC with JSP</h1>
<%
/* various variable declarations */
Connection dbc;
Statement st;
String sql;
ResultSet rs;
ResultSetMetaData rsmd;

try {
  /* load JDBC driver for PostgreSQL database */
  Class.forName("postgresql.Driver");

  /* make a connection to the database */

dbc=DriverManager.getConnection("jdbc:postgresql://localhost/webtest","www-d
ata","");

  /* create and execute a statement */
  st=dbc.createStatement();
  sql=("SELECT * FROM example");
  st.executeQuery(sql);

  /* get the results and metadata */
  rs=st.getResultSet();
  rsmd=rs.getMetaData();

  /* how many columns are there? */
  int numcols=rsmd.getColumnCount();

  /* start table and print headings */
  out.println("<table>\n<thead>\n<tr>");
  int i;
  for (i=1;i<=numcols;i++)
    out.print("<th>"+rsmd.getColumnLabel(i)+"</th>");
  out.println("</tr>\n</thead>\n<tbody>");

  /* print the rows of the table */
  while (rs.next()) {
    out.print("<tr>");
    for (i=1;i<=numcols;i++)
      out.print("<td>"+rs.getObject(i)+"</td>");
    out.print("</tr>");
    }

  /* end table and close DB */
  out.print("</tbody>\n</table>\n");
  dbc.close();

  /* error handling */
  } catch (Exception e) {
    out.println("<p>Error in JDBC database access</p>");
    out.println("<p>"+e+"</p>");
  } ;

%>
</body>
</html>

Thanks again,

G :)

Public Sub House()

On Error Resume drink

         If PintGlass.empty = True Then
                 PintGlass.refill
   Else
                 PintGlass.drink
         End if

stomach.add PintGlass

MsgBox " I've had .... " & stomach.count & " Pints"
MsgBox "VERY DRUNK"

End Sub


_________________________________________________________________
Join the world's largest e-mail service with MSN Hotmail.
http://www.hotmail.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

==========================================================================To 
unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to