Title: RE: SQL insert with preparedStatement

Daniel,

        You don't add the column name that auto increments to your insert statements, it auto populates it.  So, if you had 4 columns, and they were named "firstname, lastname, email, userid" and userid is your auto incrementing field; then your insert statement looks like this, "INSERT INTO tablename (firstname, lastname, email) VALUES ('john','doe','[EMAIL PROTECTED]');" and it when it inserts the line it does the userid automatically. :-)

-----------------------------------------------------------------
J a m e s   B.   S w a r t
Agere Systems - Colorado Design Centers
Unix/Windows Systems Administrator, WAN, LAN,
Desktop, Voice, Printers, Security Focal Point,
& Local CIO Service Delivery Manager Contact
VOICE: 720-494-2330 ¿ FAX: 720-494-2331


-----Original Message-----
From: Daniel Jaffa [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 07, 2002 8:44 AM
To: [EMAIL PROTECTED]
Subject: Re: SQL insert with preparedStatement


This is more a database question and not much of a jsp question
but here is the answer

addSQL="INSERT INTO location (put all the database column names, minus the
field that is auto_increment.  Comma Delimented) VALUES (?, ?, ?, ?, ?, ?,?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

You should always name the fields that you are adding.

----Original Message Follows----
From: "Tiffany C." <[EMAIL PROTECTED]>
Hello,

I'm trying to Insert a new row into a MySQL database
using a preparedStatement.  However one of the fields
in the database is an auto_increment primary key.  I
want to insert the all other fields into the database
and let the database enter the auto_increment primary
key.  How do I do this?

I currently have:

String addSQL="";
addSQL="INSERT INTO location VALUES (?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement addLocation =
conn.prepareStatement(addSQL);

addLocation.setString(1, locationName);
addLocation.setString(2, region);
addLocation.setString(3, address1);
addLocation.setString(4, address2);
addLocation.setString(5, address3);
addLocation.setString(6, address4);
addLocation.setString(7, address5);
addLocation.setString(8, address6);
addLocation.setString(9, address7);
addLocation.setString(10, address8);
addLocation.setString(11, address9);
addLocation.setString(12, address10);
addLocation.setString(13, phoneNumber);
addLocation.setString(14, faxNumber);
addLocation.setString(15, manager1);
addLocation.setString(16, manager2);
addLocation.setString(17, director);

//18 is the auto_increment Primary Key

addLocation.setString(19, website);
addLocation.setString(20, chart);
addLocation.executeUpdate();
addLocation.close();

And it results in
java.lang.ArrayIndexOutOfBoundsException

How can I make this work?

Sincerely,
Tiffany

__________________________________________________
Do You Yahoo!?
Send FREE video emails in Yahoo! Mail!
http://promo.yahoo.com/videomail/

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

  http://archives.java.sun.com/jsp-interest.html
  http://java.sun.com/products/jsp/faq.html
  http://www.esperanto.org.nz/jsp/jspfaq.jsp
  http://www.jguru.com/faq/index.jsp
  http://www.jspinsider.com


_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to