Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use data from a form to query Access database using JSP and SQL

Status
Not open for further replies.

rubertoga

Technical User
Jan 4, 2003
22
US
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=&quot;java&quot; contentType=&quot;text/html&quot;
import=&quot;ShoppingBasket,Product&quot;
%>

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

<table width=&quot;385&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot;>
<tr>
<td colspan=&quot;4&quot;><b><u>Search Results</u></b></td>
</tr>
<tr>
<td colspan=&quot;4&quot; align=&quot;right&quot;>
<a href=&quot;<%= response.encodeURL(&quot;shop-basket.jsp&quot;) %>&quot;>
<img src=&quot;images\viewbasket.gif&quot; border=&quot;0&quot; alt=&quot;View Basket&quot;></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(&quot;title&quot;);
String surname2 = request.getParameter(&quot;surname&quot;);

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

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

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

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

</table>

<jsp:useBean id=&quot;basket&quot; class=&quot;ShoppingBasket&quot; scope=&quot;session&quot;/>

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

</body>
</html>

 
Clue, have since discovered that problem is not with SQL code as it works fine if the following is taken out of the code:
<jsp:useBean id=&quot;basket&quot; class=&quot;ShoppingBasket&quot; scope=&quot;session&quot;/>

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

Therefore, problem must be in this section of code. Any ideas?
 
If your still getting the &quot;null pointer&quot; exception the stack trace should point you to the exact line of code in the JSP built .java file that produces the error. The file is in your JSP “work” folder and the filename is included in the stack trace message.

Open the file and go to the offending line of code, if it does not help you then post the line of code here for us to see.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top