Database number of columns out of limits

Solved
rapidegoyes Posted messages 3 Registration date   Status Member Last intervention   -  
rapidegoyes Posted messages 3 Registration date   Status Member Last intervention   -
Hello,
I am coding a small program with NetBeans Java and Postgres under pgAdmin3.

I have a table with 5 columns (id, Name, First name, Phone, Email).

The connection is established, but the app crashes with the following error message:

org.postgresql.util.PSQLException: The column index is out of bounds: 5, number of columns: 4.

From what I can see, I count 5 columns!!!

I kindly ask for your help, I have been working on this for 5 evenings
best regards and thank you

rapidegoyes

Here is my function:

 private void but_EnregistActionPerformed(java.awt.event.ActionEvent evt) 
{
try
{

String requete = "INSERT INTO renseignements (Nom, Prenom, Telephone, Courriel)";
requete+= "VALUES (?,?,?,?);";

PreparedStatement ps = connect.getInstance().prepareStatement(requete);

ps.setString(2, txt_Nom.getText());
ps.setString(3, txt_Prenom.getText());
ps.setString(4, txt_Telephon.getText());
ps.setString(5, txt_Mail.getText());

ps.executeUpdate();

JOptionPane.showMessageDialog(this, "Query executed successfully");

} catch (SQLException ex)
{
Logger.getLogger(Interface_Utili.class.getName()).log(Level.SEVERE, null, ex);
}
}


Configuration: Windows / Firefox 96.0

5 answers

  1. jordane45 Posted messages 30427 Registration date   Status Moderator Last intervention   4 833
     
    Hello;

    Knowing that in your query you only pass 4 variables (symbolized by question marks)
    I doubt that ps.setString(5) is correct... And you're missing ps.setString(1)

    --

    Best regards,
    Jordane
    1
  2. rapidegoyes Posted messages 3 Registration date   Status Member Last intervention   13
     
    Good evening
    thank you for responding

    I already tried with 1 so the error message is different as follows:

    org.postgresql.util.PSQLException: ERROR: column "name" of relation "information" does not exist

    really I can’t see a solution

    Thank you for helping me sincerely
    rapidgoyes
    1
    1. jordane45 Posted messages 30427 Registration date   Status Moderator Last intervention   4 833
       
      Are you sure your database field is named "Nom" and not "nom"?
      Could you show us the exact structure of your table?
      0
  3. rapidegoyes Posted messages 3 Registration date   Status Member Last intervention   13
     
    Hello
    Thank you for your support

    Yes, I am sure about the column title which comes after the id column; it is indeed Name with a capital N.
    Now, how do I display my table that is located on Postgres Admin 3?

    Best regards, see you soon
    rapidegoyes
    1
  4. rapidegoyes Posted messages 3 Registration date   Status Member Last intervention   13
     
    Hello jordane45

    In the meantime, I modified my code.
    1) I renamed my columns to lowercase.
    2) I modified my function as below, using lowercase and adding a value for id in front of the question mark parameters.
    3) I reset the order numbers starting from 1
    4) I compiled with clean.
    This time, the data is being transmitted into the respective columns!

    My question is something I don't understand; for the id column.
    1) Can it be automatically incremented like in SQLite Android?
    2) Or do I need to fill it in for every record?

    For my test in the code below, I added the number 1 for testing and now the data is inserted!

    Here is my modified function

    private void but_EnregistActionPerformed(java.awt.event.ActionEvent evt) 
    {
    try
    {

    String requete = "INSERT INTO renseignements (id, nom, prenom, telephone, courriel)";
    requete+= "VALUES (1, ?,?,?,?);";

    PreparedStatement ps = connect.getInstance().prepareStatement(requete);

    ps.setString(1, txt_Nom.getText());
    ps.setString(2, txt_Prenom.getText());
    ps.setString(3, txt_Telephon.getText());
    ps.setString(4, txt_Mail.getText());

    ps.executeUpdate();

    JOptionPane.showMessageDialog(this, "Request executed successfully");

    } catch (SQLException ex)
    {
    Logger.getLogger(Interface_Utili.class.getName()).log(Level.SEVERE, null, ex);
    }
    }


    Thank you for helping me
    See you soon
    rapidego
    1
  5. rapidegoyes Posted messages 3 Registration date   Status Member Last intervention   13
     
    Good evening jordan45 and everyone

    Thank you for your help,
    I completely redid my information table
    I just fixed the automatic incrementation in Postgres pgAdmin4.
    I added a sequence: on the primary key named maseq nextval('maseq'::regclass) with data type bigint.

    Apparently it's working, everything increments

    In any case, thank you for your help and see you soon
    Best regards
    rapidegoyes
    1