GeeCON Prague 2014*
The moose likes JSP and the fly likes Problem with syntax? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Problem with syntax?" Watch "Problem with syntax?" New topic
Author

Problem with syntax?

Stir Rini
Greenhorn

Joined: Apr 10, 2006
Posts: 7
Hi, I'm trying to write the values from the recordset into html fields but struggling with the syntax. Could someone please help. I'm getting this Error :
================================================
Generated servlet error:
user cannot be resolved
================================================
Here is the code:
------------------------------------------------

<html>
<head>
<%@ page
import = "java.io.*"
import = "java.lang.*"
import = "java.sql.*"
%>
<title>
Edit User
</title>
</head>
<body>
<h1><font color="#CC3300" face="Verdana" size="3">Edit User</font></h1>
<%

Connection dbconn;
ResultSet results;
PreparedStatement sql;
Statement stmt;
String url = "jdbc:mysql://localhost:3306/customers";
try
{
Class.forName("com.mysql.jdbc.Driver");
try
{
Stringuser, UId, test;
booleandoneheading = false;
dbconn = DriverManager.getConnection(url, "root", "password");
UId = request.getParameter("uid");
sql = dbconn.prepareStatement("SELECT * FROM users where username='"+UId+"'");

results = sql.executeQuery();
if(results.next())
{
user = results.getString("username");

}
else
{
out.println("not found");
}

}
catch (SQLException s)
{
out.println("SQL Error");
}
}
catch (ClassNotFoundException err)
{
out.println("Class loading error");
}
%>
<img border="0" src="intro.1.jpg"></h1>
<h1>
<font face="Arial" size="5" color="#FF9900"> User Registration Update
</font>
</h1>
<p>

<form action="http://localhost/updateusers.jsp">
<table border="0" width="80%" id="table1" cellpadding="3" cellspacing="0">
<tr>
<td width="133"><font size="2" face="Arial" color="#CC3300"> username:</font></td>
<td>
 <input type=text name=user size="50"value=<%=user%>> </td> <!--this is where it breaks-->

</tr>

</table>
</body>
</html>
[ April 10, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Check your block structure. The user variable is out of scope when you are trying to reference it.

This is only one of the very many reasons that code like this does not belong on a JSP page and should be factored out into a servlet controller or Javabean.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stir Rini
Greenhorn

Joined: Apr 10, 2006
Posts: 7
Thanks Bear,

I'm very new to java and only heard about JavaBeans. Why can't I use this JSP page? It gets the data from MySQL perfectly. I just need to find the way how to place it into html fields. I thought that this is a very common simple thing for JSP/JDBC...
IM Mahesh
Greenhorn

Joined: Mar 09, 2006
Posts: 18
Hi Stir Rini,

As u know the code u put in scriplets (i. e. <% %> goes in the _jspService method of the servlet generated. Now in ur code, u have declared the variable user in a try catch block hence it would not be visible outside of that block. Just declare the user variable out side along with the variables 'ResultSet' and 'Statement', the error will vanish.

Hope this will help u understand.


Mahesh
Stir Rini
Greenhorn

Joined: Apr 10, 2006
Posts: 7
Thanks Mahesh,

I've changed it as you suggested but now i'm getting this error:

Generated servlet error:
The local variable user may not have been initialized
IM Mahesh
Greenhorn

Joined: Mar 09, 2006
Posts: 18
Just initialize it to null.(String user = null).

It would very helpful and interesting for u to see the generated servlet code. It will give u understanding of jsp and u'll be aware of the actual code being generated out of ur jsp. Try it out once.
Stir Rini
Greenhorn

Joined: Apr 10, 2006
Posts: 7
Thanks Mahesh,

I just changed the code once again (as u suggested) and it works fine now:
Thanks a lot for your help...

<html>
<head>

<title>
Edit User
</title>
</head>
<body>
<%@ page language="java"
import="java.sql.*"
import = "java.io.*"
import = "java.lang.*"%>

<%!
// define variables
String UId;
String user;

// define database parameters
Connection dbconn;
ResultSet results;
PreparedStatement sql;
Statement stmt;
String url = "jdbc:mysql://localhost:3306/customers";
%>

<h1><font color="#CC3300" face="Verdana" size="3">Edit User</font></h1>
<%
try
{
Class.forName("com.mysql.jdbc.Driver");
try
{

booleandoneheading = false;
dbconn = DriverManager.getConnection(url, "root", "password");
UId = request.getParameter("uid");
sql = dbconn.prepareStatement("SELECT * FROM users where username='"+UId+"'");

results = sql.executeQuery();
if(results.next())
{
user = results.getString("username");

}
else
{
out.println("not found");
}

}
catch (SQLException s)
{
out.println("SQL Error");
}
}
catch (ClassNotFoundException err)
{
out.println("Class loading error");
}
%>
<img border="0" src="intro.1.jpg"></h1>
<h1>
<font face="Arial" size="5" color="#FF9900"> User Registration Update
</font>
</h1>
<p>

<form action="http://localhost/updateusers.jsp">
<table border="0" width="80%" id="table1" cellpadding="3" cellspacing="0">
<tr>
<td width="133"><font size="2" face="Arial" color="#CC3300"> username:</font></td>
<td>
 <input type=text name=user size="50"value="<%=user%>">  </td>
</tr>

</table>
</body>
</html>
 
GeeCON Prague 2014
 
subject: Problem with syntax?