Java 如何在不使用INSERT into SELECT语句的情况下实现该程序以导入到表中?

Java 如何在不使用INSERT into SELECT语句的情况下实现该程序以导入到表中?,java,mysql,sql,jdbc,Java,Mysql,Sql,Jdbc,目前,导入/插入过程工作正常。但我不想编写一个用于插入和选择的查询,而是编写一个用于从“snomed\u descriptiondata”表中选择的单独查询,以及用于插入到“snomedinfo”数据表的单独查询 我当前的代码: package Snomed.Snomed; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Date; import catalog.Root; pub

目前,导入/插入过程工作正常。但我不想编写一个用于插入和选择的查询,而是编写一个用于从“snomed\u descriptiondata”表中选择的单独查询,以及用于插入到“snomedinfo”数据表的单独查询

我当前的代码:

package Snomed.Snomed;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

import catalog.Root;

public class Snomedinfo {
  public void snomedinfoinsert()
    {
    Root oRoot = null;
    ResultSet oRsSelect = null;
    PreparedStatement oPrStmt = null;
    PreparedStatement oPrStmt2 = null;
    PreparedStatement oPrStmtSelect = null;
    String strSql = null;
    String strSql2 = null;
    String snomedcode=null;
    ResultSet oRs = null;
    String refid = null;
    String id = null;
    String effectivetime = null;
    String active = null;
    String moduleid = null;
    String conceptid = null;
    String languagecode = null;
    String typeid = null;
    String term = null;
    String caseSignificanceid = null;
    int count = 0;
    final int batchSize = 1000;
    try{
    oRoot = Root.createDbConnection(null);
    strSql = "SELECT  id FROM snomed_conceptdata WHERE active=1 ";
    oPrStmt2 = oRoot.con.prepareStatement(strSql);
    oRsSelect = oPrStmt2.executeQuery();
    String sql = "INSERT INTO snomedinfo_data (refid,id,effectivetime,active,moduleid,conceptid,languagecode,typeid,term,caseSignificanceid)SELECT refid,id,effectivetime,active,moduleid,conceptid,languagecode,typeid,term,caseSignificanceid from snomed_descriptiondata WHERE conceptid =? AND active=1" ; 
    oPrStmtSelect = oRoot.con.prepareStatement(sql);
    while (oRsSelect.next()) {
        snomedcode = Root.TrimString(oRsSelect.getString("id"));
        //String sql = "INSERT INTO snomedinfo_data (refid,id,effectivetime,active,moduleid,conceptid,languagecode,typeid,term,caseSignificanceid)SELECT refid,id,effectivetime,active,moduleid,conceptid,languagecode,typeid,term,caseSignificanceid from snomed_descriptiondata WHERE conceptid =? AND active=1" ; 
        //oPrStmtSelect = oRoot.con.prepareStatement(sql);
        oPrStmtSelect.setString(1,snomedcode);

        oPrStmtSelect.executeUpdate();                 
        }

    //oPrStmtSelect.executeBatch();
    System.out.println("done");
    }

    catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        oRsSelect = Root.EcwCloseResultSet(oRsSelect);
        oRs = Root.EcwCloseResultSet(oRs);
        oPrStmt = Root.EcwClosePreparedStatement(oPrStmt);
        oPrStmt = Root.EcwClosePreparedStatement(oPrStmt2);
        oPrStmt = Root.EcwClosePreparedStatement(oPrStmtSelect);
        oRoot = Root.closeDbConnection(null, oRoot);
    }
}
    public static void main(String args[] ) throws Exception 
      { 
      Snomedinfo a = new Snomedinfo();
      a .snomedinfoinsert();
      }
} 
注意:如何在不使用嵌套while循环的情况下执行此操作?有人能提供符合我的程序的解决方案吗?

您可以(并且应该)在
in
子句中包含概念id选择查询:

INSERT INTO snomedinfo_data (refid, id, effectivetime, active, moduleid, conceptid, 
                             languagecode, typeid, term, caseSignificanceid) 
    SELECT refid, id, effectivetime, active, moduleid, conceptid, 
                    languagecode, typeid, term, caseSignificanceid 
           FROM snomed_descriptiondata 
           WHERE active = 1 AND conceptid IN 
               (SELECT cd.id FROM snomed_conceptdata cd WHERE cd.active = 1)

通过这种方式,您应该能够在一条语句中完成所有操作,这将比JDBC驱动程序逐行处理相同的数据(又称慢处理慢处理)快几个数量级。

使用内部查询。不,查询必须是分开的,我不想将它们作为单个查询来编写。.无关:a)阅读java命名约定。类名是大写的,方法是camelCase()B)使用有意义的名称,并避免使用缩写。最后:你希望别人花时间帮助你。因此,请正确格式化/缩进您的输入,而不是转储这种延迟缩进的代码。最好遵循MVC设计模式。不,查询必须是分开的,我不想把它们写成一个查询!!因此,程序中总共应该有3个查询。两个select查询和一个insert查询您能解释一下为什么查询需要分开吗?您对中间结果做了什么?问题是我不知道如何在不使用嵌套while循环的情况下执行此操作从snomed\u conceptdata表中检索“id”后,iam仅从snomed\u descriptiondata中选择列conceptid=id的记录