Please help!

Please help with code:

<%
String connectionURL = "jdbc:mysql://localhost:3306/xxxxx";
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection connection = java.sql.DriverManager.getConnection(connectionURL, "xxxx", "xxxx");
 
String strSQL_Select = "SELECT * FROM user WHERE username = '"+request.getParameter("username")+"'";
 
java.sql.Statement statement = connection.createStatement();
 
java.sql.ResultSet RS = statement.executeQuery(strSQL_Select);
 
int i = 0;
while (RS.next())
{
 i++;
}
 
if (i == 0)
{
 
String query = "INSERT INTO user (username,password,address1,address2,city,postcode,number,name,surname,email) VALUES (?,?,?,?,?,?,?,?,?,?)";
java.sql.PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1,request.getParameter("username"));
statement.setString(2,request.getParameter("password"));
statement.setString(3,request.getParameter("address1"));
statement.setString(4,request.getParameter("address2"));
statement.setString(5,request.getParameter("city"));
statement.setString(6,request.getParameter("postcode"));
statement.setString(7,request.getParameter("number"));
statement.setString(8,request.getParameter("name"));
statement.setString(9,request.getParameter("surname"));
statement.setString(10,request.getParameter("email"));
statement.executeUpdate();
 
}
else
{
 out.println("Username has been used!");
}
 
connection.close();
%>

I get an error: :(:(:(:(:frowning:

The server encountered an internal error () that prevented it from fulfilling this request. exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 74 in the jsp file: /ProcessInsertUser1.jsp
Duplicate local variable statement
71: {
72:
73: String query = “INSERT INTO user (username,password,address1,address2,city,postcode,number,name,surname,email) VALUES (?,?,?,?,?,?,?,?,?,?)”;
74: java.sql.PreparedStatement statement = connection.prepareStatement(query);
75: statement.setString(1,request.getParameter(“username”));
76: statement.setString(2,request.getParameter(“password”));
77: statement.setString(3,request.getParameter(“address1”));

Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:319)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:302)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

Thank you.

The compiler said “Duplicate local variable statement”.

You have 2 variables named ‘statement’

java.sql.Statement statement = connection.createStatement();

java.sql.PreparedStatement statement = connection.prepareStatement(query);

FYI, this line:

String strSQL_Select = “SELECT * FROM user WHERE username = '”+request.getParameter(“username”)+“'”;

Is prone to SQL injection (e.g. ?username=‘’;DELETE FROM user)… you should use a prepared statement for that query as well.

Hi, Thank you, I’ve changed second statement to pstatement:

<%
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/nafanya6_database", "nafanya6_krolik6", "kotik2410");
java.sql.Statement statement = connection.createStatement();

String strSQL_Select = "SELECT * FROM user WHERE username = '"+request.getParameter("username")+"'";
 
java.sql.Statement statement = connection.createStatement();
 
java.sql.ResultSet RS = statement.executeQuery(strSQL_Select);
 
int i = 0;
while (RS.next())
{
 i++;
}
 
if (i == 0)
{
 
String query = "INSERT INTO user (username,password,address1,address2,city,postcode,number,name,surname,email) VALUES (?,?,?,?,?,?,?,?,?,?)";
java.sql.PreparedStatement pstatement = connection.prepareStatement(query);
pstatement.setString(1,request.getParameter("username"));
pstatement.setString(2,request.getParameter("password"));
pstatement.setString(3,request.getParameter("address1"));
pstatement.setString(4,request.getParameter("address2"));
pstatement.setString(5,request.getParameter("city"));
pstatement.setString(6,request.getParameter("postcode"));
pstatement.setString(7,request.getParameter("number"));
pstatement.setString(8,request.getParameter("name"));
pstatement.setString(9,request.getParameter("surname"));
pstatement.setString(10,request.getParameter("email"));
pstatement.executeUpdate();
 
}
else
{
	out.println("Username has been used!");
}

connection.close(); 
%>

BUT GETTING THESE:

Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /register.jsp
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:433)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:361)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.io.FileNotFoundException: /register.jsp
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:278)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.28 logs.

CAN YOU HELP? WHAT SHOULD I DO?
THANK YOU

do you have a file called ‘register.jsp’?
based on your other logs, it should be on the same level as ‘ProcessInsertUser1.jsp’…

@JURN.

Register.jsp ana ProcessInsertUser1.jsp are the same, i just tried various things and saved them under different names.

HERE’ S the code and error log again:


<%@ page import=“java.sql." %>
<%@ page import="java.io.
” %>
<%
Class.forName(“com.mysql.jdbc.Driver”);
java.sql.Connection connection = java.sql.DriverManager.getConnection(“jdbc:mysql://localhost:3306/xxx”, “xxx”, “xxx”);
java.sql.Statement statement = connection.createStatement();

String strSQL_Select = “SELECT * FROM user WHERE username = '”+request.getParameter(“username”)+“';”;

java.sql.Statement statement = connection.createStatement();

java.sql.ResultSet RS = statement.executeQuery(strSQL_Select);

int i = 0;
while (RS.next())
{
i++;
}

if (i == 0)
{

String query = “INSERT INTO user (username, password, address1, address2, city, postcode, number, name, surname) VALUES (?,?,?,?,?,?,?,?,?);”;
java.sql.PreparedStatement pstatement = connection.prepareStatement(query);
pstatement.setString(1,request.getParameter(“username”));
pstatement.setString(2,request.getParameter(“password”));
pstatement.setString(3,request.getParameter(“address1”));
pstatement.setString(4,request.getParameter(“address2”));
pstatement.setString(5,request.getParameter(“city”));
pstatement.setString(6,request.getParameter(“postcode”));
pstatement.setString(7,request.getParameter(“number”));
pstatement.setString(8,request.getParameter(“name”));
pstatement.setString(9,request.getParameter(“surname”));
pstatement.executeUpdate();

}
else
{
out.println(“Username has been used!”);
}

connection.close();
%>
<br />
<h1>Register</h1>
<br />
<form method=“Post” action=“register.jsp”>


Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /register.jsp
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:433)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:361)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.io.FileNotFoundException: /register.jsp
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:278)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.28 logs.
:frowning: :frowning: :frowning:

did you use Register.jsp or register.jsp?

@JURN low case everywhere…

FORM is inside register.jsp file, or should it be separate?

I don’t know about how you intend the user to navigate from jsp to jsp so can’t comment on that.

Check that have deployed the file register.jsp, and it is in lowercase.

and could you paste a full stack trace… normally I’d expect a plain 404 error.