This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes problem in retrieving data from a DB to HTML through JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem in retrieving data from a DB to HTML through JSP" Watch "problem in retrieving data from a DB to HTML through JSP" New topic
Author

problem in retrieving data from a DB to HTML through JSP

Prakash Chidambaram
Greenhorn

Joined: Oct 17, 2000
Posts: 7
Hi everybody,
I am doing a project in Java which involves JSP, HTML and MSAccess. I tried to add new values to a table using HTML. I used (method=post) and it worked fine. But when i tried to retrieve data from a database, and try to modify the same.. i get stucked there since i doesn't have idea regarding sending data to a HTML file from JSP. Pls. help me in this regards.
Thanks,
Prakash.

My code:
-------
<h1><font color="#0000ff">Welcome</font></h1>
<font color="#ff0000" face="Courier New">
<center>
<%@ page language="java" import="java.util.*,java.io.*,java.sql.*" %>
<%!
String name;
int age;
String output;
String storeToDatabase() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:sara","","");
PreparedStatement ps=con.prepareStatement("insert into person (name,age) values (?,?)");
ps.setString(1,name);
ps.setInt(2,age);
ps.executeUpdate();
output="are stored in the database...";
con.close();
} catch (Exception e) {}
return output;
}
%>
<%
String s=request.getParameter("submit");
if (s.equals("Add")) {
name=request.getParameter("name");
age=Integer.parseInt(request.getParameter("age"));
out.println("The Data ..."+"<br>");
out.println(" Name :"+name+"<br>");
out.println(" Age :"+age+"<br>");
out.println(storeToDatabase());
}
%>
</font>
</center>
</body>
</html>
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
Hi Prakash.Just as you used a Insert statement to put values in the DB,use the 'Select' statement to retrieve values from a DB.On executing a Select statement,you will obtain a ResultSet object,from which you can retrieve ther values and print them in the HTML form. For example (I am giving the most common form)

For a JDBC tutorial go here
I hope that helps.
------------------
Udayan Naik
Sun Certified Java 2 Programmer
[This message has been edited by Udayan Naik (edited February 28, 2001).]


Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Prakash Chidambaram
Greenhorn

Joined: Oct 17, 2000
Posts: 7
Dear Naik,

Thanks for your reply.. But i want to set the output data to a text box in the html page.. But i couldn't achieve that.. So pls. help me.. in this regard..
Thanks a lot...
prakash.
Prakash Chidambaram
Greenhorn

Joined: Oct 17, 2000
Posts: 7
Dear Friend,
Pls. help me in this regard.. The data is obtained from a database. (for example., say employee name) .I have a doubt about how to set that value for a text box element in a html file. I use JSP. Please guide me in this regards.
My code:
-------
<%@ page language="java" import="java.sql.*,java.io.*" %>
<%!
String check="";
String name="";
int age=0;
%>
<%
String k=request.getParameter("submit");
check=request.getParameter("selectPersonName").trim();
if (k.equals("ShowValues")) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:sara","","");
PreparedStatement ps=con.prepareStatement("select name, age from person where name=?");
ps.setString(1,check);
ResultSet rs=ps.executeQuery();
rs.next();
name=rs.getString("name");
age=rs.getInt("age");
con.close();
}catch (Exception e) {}
out.println("<html><body>");
out.println("<form action='http://localhost:8080/examples/jsp/view.jsp' method='post'>");
out.println("<table width='100%'>");
out.println("<tr><td>Person Name:</td>");
out.println("<td><input type='text' name='name' value=(********I want to set the value here *********)></td>");
out.println("<tr><td>Person Age:</td>");
out.println("<td><input type='text' name='age'></td>");
out.println("<tr><td><input type='submit' name='submit' value='ShowValues'></td>");
out.println("<td><input type='submit' name='submit' value='SaveValues'></td>");
out.println("</table></form>");
out.println("<h1>Hello</h1></body></html>");
}
%>
</body>
</html>
Sandeep Jain
Ranch Hand

Joined: Oct 25, 2000
Posts: 124
That is very easy just substite the values in the field. eq
<%@ page language="java" import="java.sql.*,java.io.*" %>
<%!
String check="";
String name="";
int age=0;
%>
<%
String k=request.getParameter("submit");
check=request.getParameter("selectPersonName").trim();
if (k.equals("ShowValues")) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:sara","","");
PreparedStatement ps=con.prepareStatement("select name, age from person where name=?");
ps.setString(1,check);
ResultSet rs=ps.executeQuery();
rs.next();
name=rs.getString("name");
age=rs.getInt("age");
con.close();
}catch (Exception e) {}
out.println("<html><body>");
out.println("<form action='http://localhost:8080/examples/jsp/view.jsp' method='post'>");
out.println("<table width='100%'>");
out.println("<tr><td>Person Name:</td>");
out.println("<td><input type='text' name='name' value="+name+"></td>");
out.println("<tr><td>Person Age:</td>");
out.println("<td><input type='text' name='age'"+age+"></td>");
out.println("<tr><td><input type='submit' name='submit' value='ShowValues'></td>");
out.println("<td><input type='submit' name='submit' value='SaveValues'></td>");
out.println("</table></form>");
out.println("<h1>Hello</h1></body></html>");
}
%>
</body>
</html>
But what I feel is instad of this u should follow the following code.
<%@ page language="java" import="java.sql.*,java.io.*" %>
<%!
String check="";
String name="";
int age=0;
String k=request.getParameter("submit");
check=request.getParameter("selectPersonName").trim();
if (k.equals("ShowValues"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:sara","","");
PreparedStatement ps=con.prepareStatement("select name, age from person where name=?");
ps.setString(1,check);
ResultSet rs=ps.executeQuery();
rs.next();
name=rs.getString("name");
age=rs.getInt("age");
con.close();
}
catch (Exception e) {}
%>
<html><body>
<form action='http://localhost:8080/examples/jsp/view.jsp' method='post'>"
<table width='100%'>
<tr><td>Person Name:</td>
<td><input type='text' name='name' value=<%=name%> ></td>
<tr><td>Person Age:</td>
<td><input type='text' name=' age=<%=age%> ></td>
<tr><td><input type='submit' name='submit' value='ShowValues'></td>
<td><input type='submit' name='submit' value='SaveValues'></td>
</table></form>
<h1>Hello</h1></body></html>
<%
}
%>
</body>
</html>


------------------
Sandeep Jain


Try and Try Till u succeed<br /> <br />Sandeep Jain
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in retrieving data from a DB to HTML through JSP
 
Similar Threads
in which method shuld i use at the servlet code for request
Servlet becomming slow
Testing Cookies
Servlet becomming slow
configuring tomcat to use MsAcess