Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Distinct names for multiple buttons

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

I have been working on a shopping site. i need to create different number of buttons in the jsp page according to the database.

this is my code
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<script language="javascript">

</script>

<title>Solitude:Furniture-3</title>
</head>
<body>
<%
try {

String connectionURL = "jdbc:mysql://localhost:3306/login";
Connection connection = null;
Statement statement = null;
ResultSet rs= null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
statement = connection.createStatement();
String QueryString = "SELECT * from revolvingchair";
rs = statement.executeQuery(QueryString);
String s[];
int i=10;
%>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<thead>
<th>Item ID</th> <th>Item Name</th> <th>Item Description</th> <th>Item Price</th> <th>Item Available</th> <th>Quantity</th>
</thead>
<%

while (rs.next()) {
%>
<TR>
<TD><%=rs.getInt(1)%></TD>
<TD><%=rs.getString(2)%></TD>
<TD><%=rs.getString(3)%></TD>
<TD><%=rs.getInt(4)%></TD>
<TD><%=rs.getInt(5)%></TD>
<TD><input name="<% rs.getString(2);%>" type="submit" value="AddtoCart" /></TD></TR>
<% i++;
} %>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {


out.println("Unable to connect to database.");
}
%>

<input type="submit" value="Add" name="AddtoCart" ></input>

</TABLE><TABLE>
<TR>

</TR>
</TABLE>
</form>
</body>
</html>

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

My particular problem is i cannot differentiate the buttons when the user clicks the name wich i have given is taking as null please help me how to declare distinct names to buttons
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its been said many times before.
Scriptlet code in a JSP is bad
Database connections in a JSP is worse.

The Database code should be in a bean.
It should run the query, and convert the results into a List of java objects.
Your JSP should receive that List of objects and display it appropriately.

I don't see a form on your page. What are you submitting and to where?

In order to distinguish which button is pressed you need to give each button a distinct value. Or search for each button by name (the former being preferable)

I would suggest doing it as a link:



and then in the addToCart servlet handling this request:

request.getParameter("item") would tell you the id of the item you are wanting to add.



 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic