简体   繁体   中英

How to implement this program for importing into a table without using the INSERT INTO SELECT Statement?

Currently the import/insert process is working perfectly. But I don't want to write a single query for both inserting and selecting but rather write a separate query for selecting from 'snomed_descriptiondata' table and separate query for inserting to 'snomedinfo'_data table.

My current code:

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();
      }
} 

Note: how do I do this without using nested while loops? Can someone provide me a solution that is in accordance with my program?

You can (and should) include the concept-id-selecting query within an IN clause:

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)

This way you should be able to do everything in one statement which will be orders of magnitude faster than processing the same data row-by-row (aka slow-by-slow) by the JDBC driver.

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