iam doing a project
but in my code it is not retrieving the data after the space
like
in data base i have " ravi teja"
then
in front end it is getting only "ravi"
so can u suggest me the code
plzzzzz

Can you post the method and only the method that you use to read the rows from the database?

@javaaddict
plzzz can u send the code for getting data from databse in jsp
i used the program from "roseindia"
but still it is not getting the data from databse after the space
plzzzzz

Can you post the code you used?

Or you can search this forum for examples on how to run queries at a database and get the results.
Remember, put that code in a separate method in a class and call that method from wherever you want. That method should return the data you want to display.

And try to avoid roseindia

commented: +1 Good suggestion. +15

"home.jsp"

<HTML>
<HEAD>
<TITLE>HOME </TITLE>
<script>
function validateForm(theForm){
if(theForm.pass.value=="")
{
alert("Enter password");
theForm.password.focus();
return false;
}
else if(theForm.username.value=="")
{
alert("enter username");
theForm.username.focus();
return false;
}
else
return true;
}
</script>
</HEAD>
<BODY bgcolor="white">
<table align="center" width="400px" border=0>
<tr><td colspan=2>&nbsp;</td></tr>
<FORM ACTION="fetch.jsp" METHOD="POST" onsubmit="return validateForm(this);">
<tr>
<td><b><font size=5>Year Of Enrollment</b></font></b></td>
<td><input type="text" name="yearenrol"></td>
<tr>
<td><b><fontsize=5>Section</b></font></b></td>
<BR>
<td><INPUT TYPE="TEXT" NAME="section"></td>
<BR>
<br><td><button type="submit"><img src="Tick-icon.png" width="30px"></td></br>
</tr>
</FORM>
<FORM ACTION="fetch1.jsp" METHOD="POST" onsubmit="return validateForm(this);">
<tr>
<td><b><font size=5>Id No.</b></font></b></td>
<td><input type="text" name="username"></td></tr>
<br><td><button type="submit"><img src="Tick-icon.png" width="30px"></td></br>
</form>
</BODY>
<HTML>

"fetch1.jsp"

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %> 
<html>
<head>
<title>display</title>
</head>
<body>
<h2>Data</h2>
<%
try {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
connection = DriverManager.getConnection("jdbc:odbc:ravi3", "system", "tiger");
statement = connection.createStatement();
String yearenrol = request.getParameter("yearenrol").toString();
String section = request.getParameter("section").toString();
String QueryString = "select * from reg where yearenrol='"+yearenrol+"'and section='"+section+"'";
rs = statement.executeQuery(QueryString);
%>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<%
while (rs.next()) {
%>
<form action="detail.jsp" method="post">
<input type="text" value=<%= rs.getString(1) %> name="username"></td>
<input type="submit" value="submit" name="submit">
</form>
<% } %>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("sory ");
}
%>
</TABLE><TABLE>
</TABLE>
</font>
</body>
< /html>

this is my code
but am not getting the data after the space on retrieving,,
plzzz solve this

<FORM ACTION="fetch1.jsp" >

You submit to the fetch1.jsp and you try to get the yearenrol, ... but those fields are not in the above form. The form I have posted only has the fields: username.

Also this is wrong: String section = request.getParameter("section").toString(); You don't need the toString() . The request.getParameter already returns a String. But if no section was submitted then the request.getParameter("section") would be null so you will get a NullPointerException.

So when you submit a form only the fields inside it get sent. So if you submit the
<FORM ACTION="fetch1.jsp" >
Or the
<FORM ACTION="fetch.jsp" >
Make sure to read inside those jsps only the fields that are inside them:

<FORM ACTION="[B]fetch1.jsp[/B]" METHOD="POST" onsubmit="return validateForm(this);">
<tr>
<td><b><font size=5>Id No.</b></font></b></td>
<td>[B]<input type="text" name="username">[/B]</td></tr>
<br><td><button type="submit"><img src="Tick-icon.png" width="30px"></td></br>
</form>

The validateForm function is wrong. You use it at both forms but only one of them has the username: else if(theForm.username.value=="") So if you try to submit the other form it will give you an error. Use 2 different function for each form.

Delete the entire fetch1.jsp . Like I said, put all of your code that connects to the database to a separate method. Pass the appropriate arguments execute the query and have that method return the results. Then call that method from the jsp and display them.
The method should return a list of objects.

------------------------------
Read carefully:
If you want to write that kind of code you need to know OOP. You need to know how to create your own class with attributes the columns of the table reg . Read the data from the table, put the values or each row in your class and return a list of those objects. The above has nothing to do with jsp. You must know how to do the above. Then call that method from a main method and test it. Then call it from your jsp. First do this.

Then go at the jsp page.

And don't try to learn from random examples you found on the internet. Buy a book and learn about HTML, JSPs and servlets.

tahnks for u r suggestion,
will try
but i am not getting the data after the sapce in front end though i implemented what u said,
i think there is an error in "<rs.getstring(1)>"
plzzz check in that way

tahnks for u r suggestion,
will try
but i am not getting the data after the sapce in front end though i implemented what u said,
i think there is an error in "<rs.getstring(1)>"
plzzz check in that way

If you implemented what I said then you shouldn't be using this:
<%= rs.getstring(1)%>
Remove all the connection/database code from the jsp. Only call a method from a class that returns a list with the data. Then loop that list and display the data

<%
MyClass cl = new MyClass();
ArrayList<String> list = cl.getUsers(); 

int size = list.size();
for (int i=0;i<size;i++) {
%>

  <form action="detail.jsp" method="post">
   <input type="text" value=[B]......[/B] name="username"></td>
   <input type="submit" value="submit" name="submit">
  </form>

<%
}
%>

Do that, and we will talk what needs to be in the dots. Remember, first write the method. It must be independent. Meaning that you should be able to run it in a main method for example. The jsp part should only call that method. No java code other than what is needed for displaying.

thanks to "java addict"
i got it,,
its just a simple change.
insted of retrieving into a text box i used a table
then i got,
so text boxes cant give data after space from database i guess

so text boxes cant give data after space from database i guess

Not true. As the guy in the White House said:

Yes they can!

Use:

<input type="text" value="<%=name%>" name="username">

Instead of:

<input type="text" value=<%=name%> name="username">

If the java variable has value: "hello world" then after the loading of the page the html code rendered would be for the above cases:


Use:

<input type="text" value="hello world" name="username">

Instead of:

<input type="text" value=hello world name="username">

So the second part will display only the "hello":
value=hello world
Because it will assume that the hello is the value and the world is some other attribute the the <input /> tag

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.