开发者

INSERT SELECT JAVA JDBC

开发者 https://www.devze.com 2023-01-29 21:56 出处:网络
How do I insertall records from another table using insert select? try { PreparedStatement st=dc.getConnection().prepareStatement(\"INSERT INTO timerecord (empno)SELECT empno FROM Employeemaster WHER

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

精彩评论

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