Java
  Home arrow Java arrow Page 5 - The JSP Files (part 5): No Forwarding ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

The JSP Files (part 5): No Forwarding Address
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2001-03-19

    Table of Contents:
  • The JSP Files (part 5): No Forwarding Address
  • Dumped!
  • The Scenic Route
  • One Step At A Time
  • New Friends
  • No Forwarding Address
  • Cleaning Up

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    The JSP Files (part 5): No Forwarding Address - New Friends


    (Page 5 of 7 )

    Thus far, we've simply been using SELECT queries to pull information out of a database. But how about putting something in?

    SQL aficionados know that this happens via an INSERT query. And so, the next item on the agenda involves adding new entries to the address book. Here's the form we'll be using:


    <html> <head> <basefont face="Arial"> </head> <body> <center> <h2>Add Address Book Entry</h2> <table border=0 cellspacing=5 cellpadding=5> <form action="add_res.jsp" method="POST"> <tr> <td><b>Username</b></td> <td> <select name="uid"> <!-- generate list of available usernames from database --> <%@ page language="java" import="java.sql.*" %> <% // database parameters String host="localhost"; String user="us867"; String pass="jsf84d"; String db="db876"; String connString; // load driver Class.forName("org.gjt.mm.mysql.Driver"); // create connection string connString = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" + pass; // pass database parameters to JDBC driver Connection Conn = DriverManager.getConnection(connString); // query statement Statement SQLStatement = Conn.createStatement(); // generate query String Query = "SELECT DISTINCT uid FROM abook"; // get result ResultSet SQLResult = SQLStatement.executeQuery(Query); // get and display each record while(SQLResult.next()) { String UId = SQLResult.getString("uid"); out.println("<option>" + UId); } // close connections SQLResult.close(); SQLStatement.close(); Conn.close(); %> </select> </td> </tr> <tr> <td>First name</td> <td><input type="Text" name="fname" size="15"></td> </tr> <tr> <td>Last name</td> <td><input type="Text" name="lname" size="15"></td> </tr> <tr> <td>Address</td> <td><textarea name="address"></textarea></td> </tr> <tr> <td>Tel</td> <td><input type="Text" name="tel" size="10"></td> </tr> <tr> <td>Fax</td> <td><input type="Text" name="fax" size="10"></td> </tr> <tr> <td>Email address</td> <td><input type="Text" name="email" size="10"></td> </tr> <tr> <td>Company</td> <td><input type="Text" name="company" size="25"></td> </tr> <tr> <td>Comment</td> <td><input type="Text" name="comment" size="25"></td> </tr> <tr> <td colspan=2><input type="submit" name="submit" value="Add"></td> </tr> </form> </table> </center> </body> </html>
    If you examine it closely, you'll see that this form performs a query to retrieve the list of users currently available in the system, and uses this data to generate a list box containing the different user names. This makes it possible to specify the owner of each record when it is INSERTed.

    Once the form has been filled up and submitted, control passes to "add_res.jsp", which takes care of actually performing the INSERT operation. Take a look.

    <html> <head> <basefont face="Arial"> </head> <body> <center> <%@ page language="java" import="java.sql.*" %> <% // add_res.jsp // form data String uid = request.getParameter("uid"); String fname = request.getParameter("fname"); String lname = request.getParameter("lname"); String address = request.getParameter("address"); String tel = request.getParameter("tel"); String fax = request.getParameter("fax"); String email = request.getParameter("email"); String company = request.getParameter("company"); String comment = request.getParameter("comment"); // database parameters String host="localhost"; String user="us867"; String pass="jsf84d"; String db="db876"; String conn; Class.forName("org.gjt.mm.mysql.Driver"); // create connection string conn = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" + pass; // pass database parameters to JDBC driver Connection Conn = DriverManager.getConnection(conn); // query statement Statement SQLStatement = Conn.createStatement(); // generate query String Query = "INSERT INTO abook (id, uid, fname, lname, tel, fax, email, addr, company, comment) VALUES (NULL, '" + uid + "', '" + fname + "', '" + lname + "', '" + tel + "', '" + fax + "', '" + email + "', '" + address + "', '" + company + "', '" + comment + "')"; // get result code int SQLStatus = SQLStatement.executeUpdate(Query); if(SQLStatus != 0) { out.println("Entry succesfully added."); } else { out.println("Error! Please try again."); } // close connection SQLStatement.close(); Conn.close(); %> </center> </body> </html>
    This example demonstrates yet another method of the Statement object, the executeUpdate() method, used for INSERT or UPDATE operations. This method returns a result code indicating the number of rows affected by the operation - in case of the example above, this result code should be 1. In case it isn't...you've got trouble!

    It should be noted at this point that if your INSERT statement contains special characters which need to be escaped (say, commas or single quotes), you'd do better to use the PreparedStatement class, which automatically takes care of escaping special characters and offers some performance benefits as well. A discussion of the PreparedStatement class is beyond the scope of this tutorial, but there's plenty of documentation out there should you ever require it.

    More Java Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway