I am confused to how to get data on JSP. Please dont mind me. This is my first bean application.
This is one of the function in ToyDBAO.java class
public List<Toy> getToys()
{
try {
ResultSet rs = CM.executeQuery("SELECT * FROM Toy"); // CM is a connection object instantiated when i declare an object of ToyBDAO
while (rs.next())
{
Toy t = new Toy();
t.setToyid(rs.getString(1));
System.out.print(t.getToyid());
t.setName(rs.getString(2));
t.setPrice(rs.getInt(3));
t.setDescription(rs.getString(4));
t.setImageid(rs.getString(5));
toys.add(t); //toys is a arraylist.
}
}
catch (Exception ex)
{
System.out.print("Exception: "+ex.getMessage());
}return toys;
}
--
I call this function from one of the class called ToyDB.java
this is the function
public List getToys() {
ToyDBAO DB = new ToyDBAO();
return DB.getToys();
}
Now some one please tell me wht code should I write in jsp do display result.
1.in jsp page use <use bean id> tag to ur class.
2.create a array list in ur jsp page.
3.Arraylist object = get the values from database();//call that function which is having list objects.
5.iterate thro the arraylist using get()
4.Use <%=%> to output the result in jsp
Hi,
I am not able to clearly get ur requirements. Assuming that you have records in mysql database and needs to populate the specific column values in a list box. I am prescribing a sample jsp page.
<html>
<head>
<title> Web Application </title>
</head>
<body>
<form name="frm" method="post" action="loadlist.jsp">
<select name="lst" id="lst">
<%@ page language="java" import="java.sql.*"%>
<%
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbmaintain","root","");
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select fname from userdetail");
while (rs.next())
{
%>
<option value=<%=rs.getString(1)%>> <%=rs.getString(1)%> </option>
<%
}
%>
</select>
</form>
</body>
</html>
In above code snippet i had implemented to list the name to the listbox in the page.
Get back to me if the code doesnot work or else if you have some other requirements.
Thanks&Regards,
Michael