Issue
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"
Solution
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.
Answered By - R. Karlus
Answer Checked By - Senaida (JavaFixing Volunteer)