how do I fix a fixSQLFieldValue error
I have copied this program from a book and am trying to run it against a database that I have. Before I can do that I have one error regarding the fixSQLFieldValue. Every where this statement occurs I have an error, I am sure it has to do with the package import but I don't know how to fix it, any help would be greatly appreciated.
The error is as follow:
Compiling 1 source file to C:\Documents and Settings\Michael\TestRegistration\build\web\WEB-INF\classes
C:\Documents and Settings\Michael\TestRegistration\src\java\RegistrationServlet.java:53: cannot find symbol
symbol : method fixSQLFieldValue(java.lang.String)
location: class com.brainysoftware.java.StringUtil
" WHERE userName='" + StringUtil.fixSQLFieldValue(userName) + "'";
C:\Documents and Settings\Michael\TestRegistration\src\java\RegistrationServlet.java:66: cannot find symbol
symbol : method fixSQLFieldValue(java.lang.String)
location: class com.brainysoftware.java.StringUtil
" ('" + StringUtil.fixSQLFieldValue(firstName) + "'," +
C:\Documents and Settings\Michael\TestRegistration\src\java\RegistrationServlet.java:67: cannot find symbol
symbol : method fixSQLFieldValue(java.lang.String)
location: class com.brainysoftware.java.StringUtil
" '" + StringUtil.fixSQLFieldValue(lastName) + "'," +
C:\Documents and Settings\Michael\TestRegistration\src\java\RegistrationServlet.java:68: cannot find symbol
symbol : method fixSQLFieldValue(java.lang.String)
location: class com.brainysoftware.java.StringUtil
" '" + StringUtil.fixSQLFieldValue(userName) + "'," +
C:\Documents and Settings\Michael\TestRegistration\src\java\RegistrationServlet.java:69: cannot find symbol
symbol : method fixSQLFieldValue(java.lang.String)
location: class com.brainysoftware.java.StringUtil
" '" + StringUtil.fixSQLFieldValue(password) + "')";
5 errors
The code is as follow:
import com.brainysoftware.java.StringUtil;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class RegistrationServlet extends HttpServlet {
private String firstName = "";
private String lastName = "";
private String userName = "";
private String password = "";
public void init() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("JDBC driver loaded");
}
catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
sendPageHeader(response);
sendRegistrationForm(request, response, false);
sendPageFooter(response);
}
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
sendPageHeader(response);
firstName = request.getParameter("firstName");
lastName = request.getParameter("lastName");
userName = request.getParameter("userName");
password = request.getParameter("password");
boolean error = false;
String message = null;
try {
Connection con =
DriverManager.getConnection("jdbc:microsoft:sqlserver://TOSHIBA-USER;/*DatabaseName=Users;*/SelectMethod=cursor","sa","sa");
System.out.println("got connection");
Statement s = con.createStatement();
String sql = "SELECT UserName FROM Users" +
" WHERE userName='" + StringUtil.fixSQLFieldValue(userName) + "'";
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
rs.close();
message = "The user name <B>" + StringUtil.encodeHtmlTag(userName) +
"</B> has been taken. Please select another name.";
error = true;
}
else {
rs.close();
sql = "INSERT INTO Users" +
" (FirstName, LastName, UserName, Password)" +
" VALUES" +
" ('" + StringUtil.fixSQLFieldValue(firstName) + "'," +
" '" + StringUtil.fixSQLFieldValue(lastName) + "'," +
" '" + StringUtil.fixSQLFieldValue(userName) + "'," +
" '" + StringUtil.fixSQLFieldValue(password) + "')";
int i = s.executeUpdate(sql);
if (i==1) {
message = "Successfully added one user.";
}
}
s.close();
con.close();
}
catch (SQLException e) {
message = "Error." + e.toString();
error = true;
}
catch (Exception e) {
message = "Error." + e.toString();
error = true;
}
if (message!=null) {
PrintWriter out = response.getWriter();
out.println("<B>" + message + "</B>
");
out.println("<HR>
");
}
if (error==true)
sendRegistrationForm(request, response, true);
else
sendRegistrationForm(request, response, false);
sendPageFooter(response);
}
/**
* Send the HTML page header, including the title
* and the <BODY> tag
*/
private void sendPageHeader(HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Registration Page</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<CENTER>");
}
/**
* Send the HTML page footer, i.e. the </BODY>
* and the </HTML>
*/
private void sendPageFooter(HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("</CENTER>");
out.println("</BODY>");
out.println("</HTML>");
}
/**Send the form where the user can type in
* the details for a new user
*/
private void sendRegistrationForm(HttpServletRequest request,
HttpServletResponse response, boolean displayPreviousValues)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("
<H2>Registration Page</H2>");
out.println("
Please enter the user details.");
out.println("
");
out.println("
<FORM METHOD=POST>");
out.println("<TABLE>");
out.println("<TR>");
out.println("<TD>First Name</TD>");
out.print("<TD><INPUT TYPE=TEXT Name=firstName");
if (displayPreviousValues)
out.print(" VALUE=\"" + StringUtil.encodeHtmlTag(firstName) + "\"");
out.println("></TD>");
out.println("</TR>");
out.println("<TR>");
out.println("<TD>Last Name</TD>");
out.print("<TD><INPUT TYPE=TEXT Name=lastName");
if (displayPreviousValues)
out.print(" VALUE=\"" + StringUtil.encodeHtmlTag(lastName) + "\"");
out.println("></TD>");
out.println("</TR>");
out.println("<TR>");
out.println("<TD>User Name</TD>");
out.print("<TD><INPUT TYPE=TEXT Name=userName");
if (displayPreviousValues)
out.print(" VALUE=\"" + StringUtil.encodeHtmlTag(userName) + "\"");
out.println("></TD>");
out.println("</TR>");
out.println("<TR>");
out.println("<TD>Password</TD>");
out.print("<TD><INPUT TYPE=PASSWORD Name=password");
if (displayPreviousValues)
out.print(" VALUE=\"" + StringUtil.encodeHtmlTag(password) + "\"");
out.println("></TD>");
out.println("</TR>");
out.println("<TR>");
out.println("<TD><INPUT TYPE=RESET></TD>");
out.println("<TD><INPUT TYPE=SUBMIT></TD>");
out.println("</TR>");
out.println("</TABLE>");
out.println("</FORM>");
out.println("
");
out.println("
");
}
}
Message was edited by:
michaeldelcastillo

