ridhi
ridhi

Reputation: 45

Inserting values from JTable to MySQL

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)

Upvotes: 1

Views: 171

Answers (1)

SummerCode
SummerCode

Reputation: 1423

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.

Upvotes: 1

Related Questions