Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 21st, 2011, 12:25 PM
Registered User
 
Join Date: May 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting values from jsp form to database

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>
 
Old January 26th, 2012, 05:31 AM
Registered User
 
Join Date: Aug 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default inserting values from form to database

you should check the name of the input text box,
the name should same as you are using to retrive with the request.getParameter() method ,

example:
//in html form

name<br>
<input type="text" name= "firstname" size="30">
//
for this input type sholud be retrive in jsp as

String fname = request.getParameter("firstname");


//the name should be same in " request.getParameter("firstname")"; and

<input type="text" name= "firstname" size="30">

check it out for the next values in both html and jsp.



it works ,,try it..



String fname = request.getParameter("firstname");
String sname = request.getParameter("surname");
String email = request.getParameter("email");
 
Old November 13th, 2013, 11:34 AM
Registered User
 
Join Date: Aug 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

firstname cannot be resolved to a variable
The firstname variable is used but not declared. Declare the firstname variable.





Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting data into database using a jsp ronnie JSP Basics 4 January 15th, 2015 11:35 AM
Error while inserting values into database table pothireddy_s SQL Server 2000 2 August 3rd, 2005 03:01 AM
Inserting Values in Database OracleCommand VB.NET narendra_patil BOOK: Beginning ASP.NET 1.0 0 April 22nd, 2005 07:35 AM
inserting values into a database RPG SEARCH ASP.NET 1.0 and 1.1 Basics 3 February 5th, 2005 07:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.