#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    2
    Rep Power
    0

    Get data from form to query Access database using JSP


    I have a form that has textboxes called title2 and surname2 I'm using them to get data from the user to enter it into an SQL search for my shopping cart. I do not think the way I have called the JSP into the SQL to get the data from the form is correct (see after LIKE in SQL code) as it brings up the following error:
    Message Internal Server Error
    description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.
    exception
    java.lang.NullPointerException

    If I put an error page in the only error I get is it states "Null".

    If I input the values for the search manually i.e. LIKE '%queen%' or get all records in the Ebook table from the database without any conditions the records are displayed properly in the shopping cart, so it must be the JSP elements that are not right.

    Let me know if you need anymore information/code like the Product and shoppingbasket classes. Any help would be gratelfully appreciated.

    The SQL code for the shopping cart page is as follows:

    <%@ page language="java" contentType="text/html"
    import="ShoppingBasket,Product"
    %>

    <html>
    <head> <title>Welcome to the Shop</title></head>
    <body>

    <table width="385" border="0" cellspacing="0" cellpadding="2">
    <tr>
    <td colspan="4"><b><u>Search Results</u></b></td>
    </tr>
    <tr>
    <td colspan="4" align="right">
    <a href="<%= response.encodeURL("shop-basket.jsp") %>">
    <img src="images\viewbasket.gif" border="0" alt="View Basket"></a>
    </td>
    </tr>
    <tr>
    <td><b>Book ID</b></td>
    <td><b>Title</b></td>
    <td><b>Price</b></td>
    <td></td>
    </tr>

    <%
    String title2 = request.getParameter("title");
    String surname2 = request.getParameter("surname");

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    java.sql.Connection connection =
    java.sql.DriverManager.getConnection("jdbcdbc:Novbase","","");
    java.sql.Statement statement = connection.createStatement();
    java.sql.ResultSet RS = statement.executeQuery("SELECT * FROM Author RIGHT JOIN Ebook ON Author.author_id = Ebook.author_id WHERE Ebook.title LIKE '%" + title2 + "%' AND surname LIKE '%" + surname2 + "%'");

    int rowCounter = 0;
    while(RS.next())
    {
    String item_id = RS.getString("item_id");
    String title = RS.getString("title");
    String description = RS.getString("description");
    String price = RS.getString("price");
    String surname = RS.getString("surname");

    rowCounter++;
    String bg = (rowCounter %2 !=0) ? "#C0C0C0" : "#FFFFFF";
    %>
    <tr bgcolor="<%= bg %>">
    <td><%= item_id %><br>
    <%= surname %></td>
    <td><b><%= title %></b><br/><%= description %></td>
    <td> <%= price %></td>
    <td><a href="<%= response.encodeURL("shop-products.jsp?title="+title+"&item_id="+item_id+"&price="+price) %> ">
    <img src="images\addtobasket.gif" border="0" alt="Add To Basket"></A></td>
    </tr>
    <%
    }

    RS.close();
    connection.close();
    %>

    </table>

    <jsp:useBean id="basket" class="ShoppingBasket" scope="session"/>

    <% String title = request.getParameter("title");
    if(title!=null)
    {
    String item_id = request.getParameter("item_id");
    double price = Double.parseDouble(request.getParameter("price"));
    Product item = new Product(item_id, title, price);
    basket.addProduct( item );
    }
    %>

    </body>
    </html>
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    In this line
    Code:
    java.sql.ResultSet RS = statement.executeQuery("SELECT * FROM Author RIGHT JOIN Ebook ON Author.author_id = Ebook.author_id WHERE Ebook.title LIKE '%" + title2 + "%' AND surname LIKE '%" + surname2 + "%'");
    the title2 variable is probably null. The getParameter(String) method of the request wil return null if no variable was found in the request by that name. You should always check to see if your variables are null after retrieving them from the request.

IMN logo majestic logo threadwatch logo seochat tools logo