简体   繁体   中英

INSERT SELECT JAVA JDBC

How do I insert all records from another table using insert select?

       try {
        PreparedStatement st=dc.getConnection().prepareStatement("INSERT INTO timerecord (empno)  SELECT empno FROM Employeemaster WHERE empstatus = 'Active'" );

        i=st.executeUpdate();
        if (i>0) {
            dc.getConnection().commit();
        }
    } catch (Exception e) {
       JOptionPane.showMessageDialog(this,"Database Error: "+e.getMessage());
       return;
    }

Make sure the syntax error is not related to Java or your formatting by running the statement directly with the database before trying to go further.

Also, one of the advantages of a PreparedStatement is that you can include parameters instead of hard coding things like 'Active'. This will help prevent other syntax errors.

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