简体   繁体   English

插入选择JAVA JDBC

[英]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. 在尝试进一步操作之前,通过直接对数据库运行语句来确保语法错误与Java或格式无关。

Also, one of the advantages of a PreparedStatement is that you can include parameters instead of hard coding things like 'Active'. 另外,PreparedStatement的优点之一是您可以包含参数,而不是像“活动”这样的硬编码。 This will help prevent other syntax errors. 这将有助于防止其他语法错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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