Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 21st, 2011, 01: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>
Reply With Quote
  #2 (permalink)  
Old January 26th, 2012, 05:31 AM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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");
Reply With Quote
  #3 (permalink)  
Old November 13th, 2013, 11:34 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: , , .
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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting data into database using a jsp ronnie JSP Basics 3 March 2nd, 2014 12:09 AM
Inserting values into Access Database using OLE julius Visual Web Developer 2005 1 January 15th, 2009 06:12 PM
Error while inserting values into database table pothireddy_s SQL Server 2000 2 August 3rd, 2005 04:01 AM
Inserting Values in Database OracleCommand VB.NET narendra_patil BOOK: Beginning ASP.NET 1.0 0 April 22nd, 2005 08:35 AM
inserting values into a database RPG SEARCH ASP.NET 1.0 and 1.1 Basics 3 February 5th, 2005 07:56 PM



All times are GMT -4. The time now is 05:18 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.