0

I want to insert SQL table data from another SQL table data through NetBeans. I want that when I would to like to press actionbutton then it should be insert into SQL table data (insertdata2) from SQL table (EventLog).

Table 1 : EventLog EventId (int) ObjectId (varchar 50) Name (varchar 50) Value (varchar 50)

Table 2 : insertdata2 Id (int) ObjectId (varchar 50) Name(Varchar 50) Value(varchar50)

Here is my button code:

      DoConnect();
      st=conn.createStatement();
        rs=st.executeQuery("insert into insertdata2 (ObjectId,insertdata2.Name,insertdata.Value) select top 5 EventLog.ObjectId,EventLog.Name,EventLog.Value from EventLog order by EventId desc");           

        rs=st.executeQuery("select top 50  EventId,ObjectId,Name,Value from insertdata2 order by Id desc ");

        jTable1.setModel(net.proteanit.sql.DbUtils.resultSetToTableModel(rs)); }
 catch(Exception e){
        JOptionPane.showMessageDialog(null,e);  }               

But it's showing error : "the statement did not did not return a result set"

1
  • insert doesn't return a resultset. Use executeUpdate. Commented Feb 25, 2019 at 0:31

1 Answer 1

0

Here's the thing. You should use executeUpdate() when you trigger an insert. So you can try something like this:

final String INSERT_SQL = 
            "insert into insertdata2 (ObjectId,insertdata2.Name,insertdata.Value) select top 5 EventLog.ObjectId,EventLog.Name,EventLog.Value from EventLog order by EventId desc";
connection.prepareStatement(INSERT_SQL).executeUpdate();

rs=st.executeQuery("select top 50  EventId,ObjectId,Name,Value from insertdata2 order by Id desc ");

Not that I've executed the insert SQL before I select the new data.

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

3 Comments

Please, accept it as answer, I'm doing a job interview and my stackoverflow points count a lot.
OK , actually I am new in stackoverflow. How should I accept your answer in this question?
Thanks, you just click into the gray V icon at the left.

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.