Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a JSP variable from a resultset into a HTMl form

Status
Not open for further replies.

MPJH

Programmer
Jul 22, 2003
6
0
0
NL
Hi, I have a question regarding Resultsets in JSP.
I have a HTML form that gives 2 variables from textfields to another JSP page, which adds it to the database using the given variables in a query. Now i want the form to also include the messagenumber (this number is the primary key of the table messages). In the page I use a SQL statement (String query = "SELECT count(messageNr) FROM messages"; )to get the number of rows. In the receiving JSP page i then determine the new messageNumber by getting the value of the SQL statement and adding 1 to it.

The problem is that all of the elements work fine on their own. But when I use the Resultset to fill the HTML form it suddenly returns NULL. Here is the full source code and the error I get:

SOURCE:

<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; import=&quot;java.sql.*&quot; %>

<html>
<head>
<title>** Add message **</title>
</head>
<body>
<body bgcolor=&quot;#000000&quot; text=&quot;#FF0000&quot; link=&quot;#FFFF00&quot; vlink=&quot;#FFFF00&quot; alink=&quot;#FFFF00&quot;>


<%
Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
Connection myConn=DriverManager.getConnection(&quot;jdbc:mysql://localhost/test?user=root&password=root&quot;);
Statement stmt = myConn.createStatement();


String query = &quot;SELECT count(messageNr) FROM messages&quot;;

ResultSet resultaat = stmt.executeQuery( query );



while(resultaat.next())
{


String Countresultaat = resultaat.getString(&quot;count(messageNr)&quot;);

}


%>

<form name=&quot;formAddMessage&quot; method=&quot;post&quot; action=&quot;addMessageOut.jsp&quot;>
<CENTER>TITEL:<BR>
<input type=&quot;text&quot; name=&quot;inputMessageKop&quot; size=&quot;40&quot; maxlength=&quot;60&quot;>
<BR>
<BR>BERICHT:<BR>
<textarea name=&quot;inputMessageBody&quot; cols=&quot;50&quot; rows=&quot;20&quot;></textarea>
<BR>
<input type=&quot;hidden&quot; name=&quot;Begin&quot; value = &quot;<%= CountResultaat %>&quot;>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Post Message&quot;>
<BR>

</form>


</body>
</html>

ERROR:

cannot resolve symbol variable CountResultaat
This refers to the &quot;<input type=&quot;hidden&quot; name=&quot;Begin&quot; value = &quot;<%= CountResultaat %>&quot;> &quot; part.

Can anyone please help me?
Thanks in advance!

Menno.
 
Menno

You need to change the variable in hidden name value to Countresultaat instead of CountResultaat because that is how you have it defined as:

String Countresultaat = resultaat.getString(&quot;count(messageNr)&quot;);
 
Thanks for replying so fast, AR1982!
That was a silly typo :)

Still, when i change CountResultaat to something else (like 'a') it still gives me the same error. It looks like the variable just doesn't come out of the ResultSet. I still get the error (only then with 'a' instead of 'CountResultaat').

I read this solution on a website. I am trying to solve this 'count problem' without beans or cookies.

Thanks,
Menno.
 
Try making these changes


SELECT count(messageNr) as Count FROM messages

int Countresultaat = resultaat.getInt(&quot;Count&quot;);

String result = Countresultaat.toString();

input type=&quot;hidden&quot; name=&quot;Begin&quot; value = &quot;<%= result %>&quot;>
 
Hi,

COUNT FROM messages (or COUNT messageNr FROM messages) won't work since I use MySQL. (I tried it anyway).

When I use the following code in another file it works fine:

{
Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
Connection myConn=DriverManager.getConnection(&quot;jdbc:mysql://localhost/test?user=root&password=root&quot;);
Statement stmt = myConn.createStatement();




String query = &quot;SELECT count(messageNr) FROM messages&quot;;


ResultSet resultaat = stmt.executeQuery( query );



while(resultaat.next())
{

String aantalBerichten = resultaat.getString(&quot;count(messageNr)&quot;);
out.println(&quot; Het aantal berichten is &quot;+aantalBerichten+&quot;&quot;);


}

When I use your modifications I get the following: int cannot be dereferenced String result = Countresultaat.toString();

and the same as before: cannot resolve symbol: variable result.

But what I want to do is not use the resulting value to print on screen, but to send it (via the form) to another page. (In the other JSP page I convert the String to int by the way... using Integer.parseInt(&quot;Begin&quot;) )

That is my goal: get the result of the query and put it in a readable variable for the other page (without cookies or beans).

I hope you understand my question better now.

thanks again,
Menno.
 
well the only problem i spotted in ur old code is this:
String Countresultaat = resultaat.getString(&quot;count(messageNr)&quot;);

change it to:
String Countresultaat = resultaat.getString(&quot;messageNr&quot;);

does it work now?

Known is handfull, Unknown is worldfull
 
I'm not an expert at this but, I think you need to put the String resultaat in a declaration so that it persists through out the page.
Hope that helps...
like so <%! String resultaat; %>
 
Thanks for your responses vbkris and libaax,

the line:
String Countresultaat = resultaat.getString(&quot;count(messageNr)&quot;);

is not a problem, it delivers the data from the MySQL-database fine, I can use it in the same page perfectly.

The declaration of <%! String resultaat; %> works kind off, in the way that the error dissapears. The variable is now found by Apache. But the problem is, that the variable still is not updated by the SQL statement (to the string 200 or something) when it is sent to the next page. So the next page gives an error numberformatexception for input string: &quot;null&quot; .

I just can't seem to get the SQL value out of the page by using the HTML-form. I'll post the code so far (both pages this time):

CODE of the INPUTpage:

<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; import=&quot;java.sql.*&quot; %>

<html>
<head>
<title>** Add message **</title>
</head>
<body>
<body bgcolor=&quot;#000000&quot; text=&quot;#FF0000&quot; link=&quot;#FFFF00&quot; vlink=&quot;#FFFF00&quot; alink=&quot;#FFFF00&quot;>

<%! String result; %>
<%
Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
Connection myConn=DriverManager.getConnection(&quot;jdbc:mysql://localhost/test?user=root&password=root&quot;);
Statement stmt = myConn.createStatement();


String query = &quot;SELECT count(messageNr) FROM messages&quot;;

ResultSet resultaat = stmt.executeQuery( query );



while(resultaat.next())
{

String result = resultaat.getString(&quot;count(messageNr)&quot;);

}


%>

<form name=&quot;formAddMessage&quot; method=&quot;post&quot; action=&quot;addMessageOut.jsp&quot;>
<CENTER>TITEL:<BR>
<input type=&quot;text&quot; name=&quot;inputMessageKop&quot; size=&quot;40&quot; maxlength=&quot;60&quot;>
<BR>
<BR>BERICHT:<BR>
<textarea name=&quot;inputMessageBody&quot; cols=&quot;50&quot; rows=&quot;20&quot;></textarea>
<BR>
<input type=&quot;hidden&quot; name=&quot;Begin&quot; value = &quot;<%= result %>&quot;>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Post Message&quot;>
<BR>

</form>


</body>
</html>

CODE of the OUTPUTpage:

<%@ page language=&quot;java&quot; contentType=&quot;text/html&quot; import=&quot;java.sql.*&quot; %>

<html>
<head>
<title>** Add message **</title>
</head>
<body>

<body bgcolor=&quot;#000000&quot; text=&quot;#FF0000&quot; link=&quot;#FFFF00&quot; vlink=&quot;#FFFF00&quot; alink=&quot;#FFFF00&quot;>
<%

//het fantastische nieuwe cookie based herkennignssysteem


boolean loggedIn;
String naam;
Cookie[] cookies = request.getCookies();
naam = &quot;Nee&quot;;


if (cookies != null) {
for(int i=0; i<cookies.length; i++) {
Cookie cookie = cookies;
if (&quot;userIs&quot;.equals(cookie.getName()))
naam =(cookie.getValue());
}
}


String BeginB = request.getParameter(&quot;Begin&quot;);
int Begin = Integer.parseInt(BeginB);
int Nummer = ++Begin;

String messageKoppie = request.getParameter(&quot;inputMessageKop&quot;);
String messageBodytje = request.getParameter(&quot;inputMessageBody&quot;);



Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
Connection myConn=DriverManager.getConnection(&quot;jdbc:mysql://localhost/test?user=root&password=root&quot;);
Statement stmt = myConn.createStatement();


String query = &quot;INSERT INTO `messages` (`messageNr`, `messageKop`, `messagBody`, `author`) VALUES ('&quot;+Nummer+&quot;', '&quot;+messageKoppie+&quot;', '&quot;+messageBodytje+&quot;', '&quot;+naam+&quot;')&quot;;


ResultSet resultaat = stmt.executeQuery( query );


String redirectURL = &quot; response.sendRedirect(redirectURL);


%>

</body>
</html>

Thanks again!
Greetings,
Menno.
 
why declare result variable twice?
Also, I notice the host argument to the Database is missing port#
change this line:String result = resultaat.getString(&quot;count(messageNr)&quot;);
to :
result = resultaat.getString(&quot;count(messageNr)&quot;);
 
Hi Libaax,

port# is not nescessary since I use the default.
First I declared result just once, but then I got the error that it was not found.

I'll try your advice.

Thnx,
Menno.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top