开发者

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 开发者_JS百科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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜