Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problem with syntax?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stir Rini
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stir Rini
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
reply
    Bookmark Topic Watch Topic
  • New Topic