Bruno Kommers
Bruno Kommers

Reputation: 19

Inserting selected row into mysql

EDITED I´ve changed the SQL code and it works, but I get the exception: "cannot set a null tableModell

I´ve seen many very similar examples but not the one I need. Therefore here we go:

It´s simple - In a InternalFrame I insert Code, ProjectName, Teamleiter, Description and click "Teilnehmer" to list all members of all projects from a table "tbl_teinehmer". Just the Lastname is shown.

So, I insert the correspondent data into the fields, list the projectmembers and select the members who will take part of this new project I´m creating.

In order to insert the data and the selected row from "teilnehmer" into a new Table:

   private void buttonAnlegenActionPerformed(java.awt.event.ActionEvent evt) {                                              
            String data = tbl_teilnehmer.getValueAt(tbl_teilnehmer.getSelectedRow(), 0).toString();
            String sql = "INSERT INTO TBL_PROJETO (PROJEKT_ID, PROJEKTNAME, TEAMLEITER, BESCHREIBUNG,**TEILNEHMER**)"; //now it works but I get the exception: cannot set a null TableModell.
                    + "  VALUES(?,?,?,?,?)";
            menu = new HauptMenu();
            try{
                PreparedStatement pst = conn.prepareStatement(sql);
                pst.setString(1, ct.getText());
                pst.setString(2, nt.getText());
                pst.setString(3, st.getText());
                pst.setString(4, beschreibungText.getText());
                pst.setString(5, data);
                pst.execute();
                menu.listaProjetos();
                JOptionPane.showMessageDialog(null, "Projekt added to Database");

            }
            catch(Exception e){
                System.out.println(e);
            }
            try{
                conn.close();
            }catch(SQLException ex){
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
            ct.setText("");
            nt.setText("");
            st.setText("");

        } 

This is the error:

  java.sql.SQLException: Parameter index out of range (5 > number of parameters, which is 4).

Maybe the problem is by PreparedStatemend declaration. I´ve set

pst.setString(5, data);

as it would recognize the selected row from the variable data but I guess it´s wrong.

Upvotes: 1

Views: 103

Answers (3)

Bruno Kommers
Bruno Kommers

Reputation: 19

Thank you for helping me! I solved the problem first by changing the SQL declaration:

First at I´ve placed '"+data+"', which is an placeholder(If I´m not mistaken) but I wanted to set the selected row into the respective column in DB. Therefore I´ve changed it to the columnname: TEILNEHMER and set another questionmark '?' as VALUE.

It worked but I still got the error>> cannot set a null TableModel and under this error >> Unsupported collation 'Latin7' or smth like that.

So I´ve set the Collation of my table from Lati7 to TableDefault and now it works entirely.

THANK YOU ALL AGAIN FOR HELPING ME!

Upvotes: 1

Muhammad Husnain Tahir
Muhammad Husnain Tahir

Reputation: 1039

do it as follows

 pst.setString(0, ct.getText());
 pst.setString(1, nt.getText());
 pst.setString(2, st.getText());
 pst.setString(3, beschreibungText.getText());
 pst.setString(4, data);

Upvotes: 1

Rajesh
Rajesh

Reputation: 2155

There is no placeholder for 5th parameter in your sql query:

String sql = "INSERT INTO TBL_PROJETO (PROJEKT_ID, PROJEKTNAME, TEAMLEITER, BESCHREIBUNG,'"+data+"')"
             + "  VALUES(?,?,?,?,?)";

Upvotes: 0

Related Questions