1

I am new here and need your help ...

I'm just a beginner so bear with me for any stupidity in the code.

I have made a project on railway management system and want to insert values from text fields and java table to MySQL database. How can I get rid of this error? "column index out of range,3>1 "

Here's the code I wrote:

      try{

  Class.forName("java.sql.Driver");
  con=DriverManager.getConnection("jdbc:mysql://localhost/railway","root","ridhi");
  st =con.createStatement();

  String sourcee =(String) cb1.getSelectedItem();
  String desti =(String) cb2.getSelectedItem();
  String art =(String) cb3.getSelectedItem();
  String tic= tick.getText();
  String rdate=frm.getText();
  String rdatde=to.getText();
  String rdagte=bp.getText();
  String rdagtge=ru.getText();
  String ddate=date.getText();
  String pnr=l1.getText();

  String q="INSERT INTO pnr values("+l1.getText()+","+cb1.getSelectedItem()+","+"'"+cb2.getSelectedItem()+"'"+","+"'"+frm.getText()+"'"+","+"'"+to.getText()+"'"+","+"'"+date.getText()+"'"+","+"'"+cb3.getSelectedItem()+"'"+","+"'"+ru.getText()+"'"+","+"'"+bp.getText()+"'"+","+tick.getText()+");";

  st.executeUpdate(q);
  while(rs.next()){
     model.getRow(new Object[]{
        rs.getInt(11),rs.getString(12),rs.getInt(13), rs.getString(14),
        rs.getString(15), rs.getString(16) 
               }  );   }
         }

  catch(Exception e){
    JOptionPane.showMessageDialog(null,e.getMessage());

        }


     error: 

    java.sql.SQLException: Column count doesn't match value count at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485)
    at reservation.reservationActionPerformed(reservation.java:348)
    at reservation.access$100(reservation.java:21)
    at reservation$2.actionPerformed(reservation.java:244)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6216)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
    at java.awt.Component.processEvent(Component.java:5981)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4583)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4413)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475)
    at java.awt.Component.dispatchEvent(Component.java:4413)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
9
  • 1
    What is the error you're getting? If there's no error, what isn't working? Commented Dec 20, 2014 at 13:11
  • error is :column index out of range,3>1 Commented Dec 20, 2014 at 13:22
  • Put that in your post, and ask us a question, please. Something like "How can I get rid of this error?" Providing the full stack trace would also be nice, in PasteBin or something similar if it's too long. Commented Dec 20, 2014 at 13:24
  • i'm sorry but can you please tell me what to mean by full stack trace. Commented Dec 20, 2014 at 13:33
  • The error it gives you. The whole thing. When it spits back at you, copy/paste all of it, not just the first line. Try adding "e.printStackTrace()" to the catch block, and giving us everything that appears. Commented Dec 20, 2014 at 13:45

1 Answer 1

1

According to this post this type of error occurs when you are inserting into a table which has more columns then you provide values for; if that happens, you must reference them in parentheses by name:

INSERT INTO tablen_name (column1_name, column2_name, (more column names), column16_name) 
VALUES(value1, value2, (more values), value16);

I assume maybe you also have an id which you don't insert and is not automatically generated? Or some other columns?

Also, as a suggestion, in the insert statement you didn't need to write all those variables by hand, you could have used the references you made above, like for example replace l1.getText() with pnr and so on. (This is not causing the error, but since you just wrote them, it would be better to use them).

The code would look something like:

 String q="INSERT INTO pnr(source, destination, art, tic, rdate, rdatde, dragte, rdagtge,ddate,pnr)
  values("+pnr+","+sourcee+",'"+      desti+"','"+rdate+"','"+rdatde+"','"+
  ddate+"','"+art+"','"+rdagtge+"','"+rdagte+"',"+tic+");";

This, considering that the table's columns are called the way you called the variables earlier.

It is important that you put all the information in the format code i wrote earlier, and that should do the trick, i don't know exactly what your column names and variables containing the values are.

Sign up to request clarification or add additional context in comments.

10 Comments

corrections made.thankx.but what should be the code for inserting values from jtable to MySQL?? same as I wrote??
the code u wrote contains the variables of text field.
MySQL table structure contains 16 columns . 10 I'm inserting through textfeilds and 6 from jtable.
i assumed those variables are also the names of the columns of your table, if not, then you just need to replace them to each corresponding column...if you dont understand, please edit post to be containing your columns' names
shall I post the scrnshot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.