Sarah
Sarah

Reputation: 133

geting data from multiple table when selecting a row then clicking a button

I am try to run the code below but every time I run it, it don't work. Could any one please highlight what i am doing wrong?

What the code should do is:

I have to table in the database called r and another called sa, table r contain column said as a foreign key. in my java front end i have a Jtable in a jpanel and an update button in another jpanel. when the user select a row in the jtable and then click update. the tool will display data from r in text boxes as well as data from sa if the selected row in the jtable r have sa id as a foreign key.

the Code

               if(updateClicked == true){
                btnSubmit.setVisible(false);        
                btnUpdate.setVisible(true);
                btnNew.setEnabled(false);
                Statement st;
                PreparedStatement ps;

                ResultSet rs;   
                try {
                    String rid = table.getValue(0);
                    JOptionPane.showMessageDialog(null, rid);
                    String rq ="SELECT * FROM  `r` WHERE  'r_id`=' "+rid+"'";
                    ps = Login.con.prepareStatement(rq);
                    rs = ps.executeQuery();
                    String saID = rs.getString(2);

                    String q = "SELECT sa.Argument FROM sa, r WHERE r.sid ="+sID  ; 
                    st = Login.con.createStatement();
                    rs = st.executeQuery(q);

                    String argu = rs.getString(1);
                    System.out.println(argu);

                    if (argu.isEmpty()==false){
                        btnAddSA.doClick();
                        txtaArg.setText(argu);

                    }

                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                }

the Console output

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'R0004' in 'where clause'

Upvotes: 0

Views: 517

Answers (1)

camickr
camickr

Reputation: 324137

First of all give your tables proper names. "r" and "sa" don't mean anything.

I don't know what the problem with your SQL is but I will suggest that you use a PreparedStatement. It makes it easier to code the SQL so you don't worry about delimiter type mistakes.

A basic example would be:

String sql = "INSERT INTO Page (Name, Title) VALUES (?, ?)";

PreparedStatement stmt = connection.prepareStatement(sql);

stmt.setString( 1, "Name1" );
stmt.setString( 2, "Title1" );
stmt.executeUpdate();

You can search the forum or web for more information.

Upvotes: 1

Related Questions