Hi all,
I'm trying to insert data from a jsp website to a database created in Derby and which is embedded in Tomcat, and keep hitting brick walls. I know there's loads of stuff out there on forums etc. but none of it is helping me!
At the moment the problem is that when I try and submit the values from the form I created, I get sent to an error page which says:
An error occurred at line: 37 in the jsp file: /flowersconfirm.jsp
firstname cannot be resolved to a variable
34: st = con.prepareStatement("INSERT INTO mailing (firstname, surname, email) VALUES (?,?,?)");
35:
36:
37: st.setString(1,firstname);
38: st.setString(2,surname);
39: st.setString(3,email);
40:
And it says the same thing for my other "st.setString"s (apart from email, bizarrely).
Here is my code. ANY HELP AT ALL would be greatly appreciated. Thanks!
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Verifying</title>
</head>
<body background='flowers.png'>
<body bgcolor="#FFFFFF" text="#000000">
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="javax.naming.*,java.sql.*,javax.sql.*" %>
<%
Connection con = null;
PreparedStatement st = null;
String fname = request.getParameter("firstname");
String sname = request.getParameter("surname");
String email = request.getParameter("email");
try {
con = DriverManager.getConnection("jdbc:derby:databases/flowershopdb");
Context i = new InitialContext();
Context e = (Context) i.lookup("java:/comp/env");
DataSource d = (DataSource) e.lookup("jdbc/flowershopdb");
st = con.prepareStatement("INSERT INTO mailing (firstname, surname, email) VALUES (?,?,?)");
st.setString(1,firstname);
st.setString(2,surname);
st.setString(3,email);
st.executeUpdate();
st.clearParameters();
st.close();
con.close();
}
catch (Exception e) {
out.println("Error.."+e);
}
response.sendRedirect("successful.html");
%>
</body>
</html>