This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

problem in retrieving data from a DB to HTML through JSP

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Prakash Chidambaram
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Forget Steve. Look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic