Results 1 to 3 of 3
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default insert to MySQL table

    Hi,

    I try to insert a new post into a MySQL table.
    I have done this before with success. This time I am trying to do it a little different.
    In my other case I had a input from fields corresponding every column in my table.
    Now I have input fields to, but not for the ID. This is set to be Auto Increment in MySQL.

    The problem is that I get error when I try to insert a post. It complains about the number of columns.
    I understand this to be it needs the exact amount of columns inserted as the table has.

    Here is my insert code:
    Java Code:
    Connection connection = Connector.getConnection();
            PreparedStatement ps = null;
            
            String query = "INSERT INTO ContactTable(FirstName, LastName, Address, Zip, City, Phone, MobilePhone, Date)"
                            +" VALUES(?,?,?,?,?,?,?,?,?)";
            
                    try {
                
                ps = connection.prepareStatement(query);
                ps.setString(1, firstname);
                ps.setString(2, lastname);
                ps.setString(3, address);
                ps.setString(4, zip);
                ps.setString(5, city);
                ps.setString(6, city);
                ps.setString(7, phone);
                ps.setString(8, mobile);
                ps.setDate(9, date);
                ps.executeUpdate();
                
            } catch (SQLException ex){            
                Logger.getLogger(ContactTable.class.getName()).log(Level.SEVERE, null, ex);  
                
            } finally{
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ContactTable.class.getName()).log(Level.SEVERE, null, ex);
                }
                    }
    Any suggestions?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: insert to MySQL table

    There was not the same amount of "?"...

    But another question: After this code is run I am trying to reload the content of a table. Any suggestions of how this code should look like?

Similar Threads

  1. Replies: 5
    Last Post: 07-13-2011, 12:34 PM
  2. Not able to insert data into mysql table
    By sharanya in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 02-16-2011, 01:46 PM
  3. Replies: 1
    Last Post: 06-08-2010, 09:58 AM
  4. How to insert DATE into mysql using JSP?
    By sivakumar_sakam in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-27-2009, 07:52 PM
  5. Replies: 0
    Last Post: 07-11-2008, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •