简体   繁体   中英

Insert into SQL table data from another table data through NetBeans

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM