Oracle Sql如果存在,那么执行此操作即可

ars*_*nal 2 sql oracle

    String s1 = "create table " +tableName+
    "(id number NOT NULL PRIMARY KEY, " +
    "url varchar(1000) NOT NULL, " +
    "urlHash varchar(1000) NOT NULL, " +
    "contentHash varchar(1000), " +
    "modDate date, " +
    "contentLocation varchar(1000), " +
    "status integer, " +
    "lastCrawlDate date) ";

String s3 = "create sequence " +sequenceName+ " start with 1 increment by 1 nomaxvalue";

    stmt=conn.createStatement();
    stmt.executeUpdate(s1);
    stmt.executeUpdate(s3);

ps = conn.prepareStatement (
          "INSERT INTO testing (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate) VALUES(test_seq.nextval,?,?,?,?,?,?,?)");


          ps.setString (1, url);
          ps.setString (2, urlHash);
          ps.setString (3, contentHash);
          ps.setString (4, modDate);
          ps.setString (5, contentLocation);
          ps.setLong (6, status);
          ps.setString (7, lastCrawlDate); 
Run Code Online (Sandbox Code Playgroud)

我在这里做的是我正在创建一个表和一个自动增量序列.然后我使用预准备语句插入到oracle数据库中.此表包含大约20,000个条目的大量数据.

第一个问题: - 所以现在我要做的是如果我需要在这个表中添加任何url和其他相应的数据,我必须在表中搜索,看看这个url是否存在于这个表中.如果它不存在,则将此url添加到表和其他相应数据.那么如果存在,我怎么能实现这一点,然后在oracle sql中执行此功能.

对于第一个问题,我可以在url或urlHash上触发select查询,看看url是否存在,如果不存在则添加它

rs = stmt.executeQuery("SELECT urlHash FROM " +tableName+ " where urlHash ='urlHash' "); 
          while (rs.next()) {
              String last = rs.getString("urlHash");
              }
Run Code Online (Sandbox Code Playgroud)

然后比较这些值,如果没有比较则添加它.我不认为这是我应该采用的方式.什么是最快的方式来解决这个问题..

第二个问题 - 其次如果这个url存在并且它被修改(我们可以在最后修改的头中看到这个并且我将这个值存储在modDate中),然后用其他相应的数据更新url.

所以这是一个问题

if URL does not Exists {
Add to the oracle table and other data
} else if(url got modified by checking the modDate) {
update the url into oracle database and other data
}
Run Code Online (Sandbox Code Playgroud)

按照pilcrow解决方案进行升级: - 我正在尝试将字符串日期类型转换为日期类型,但是我在索引8处遇到缺少IN或OUT参数的错误.为什么会这样?

    ps =  conn.prepareStatement(
                        "MERGE INTO testing " +
                        "USING (  SELECT ? AS url, " +                 // We will maybe add this record
                        "                ? AS urlHash, " +
                        "                ? AS contentHash, "+
                        "         TO_DATE(?, 'YYYY-MM-DD'T'HH24:MI:SS'Z'') AS modDate, "+
                        "         ? AS contentLocation, "+
                        "         ? AS status, "+
                        "      TO_DATE(?, 'YYYY-MM-DD'T'HH24:MI:SS'Z'') AS lastCrawlDate "+
                        "           FROM dual ) maybe "+
                        "   ON (maybe.urlHash = testing.urlHash) "+
                        "         WHEN MATCHED THEN "+
                           // We only need update the fields that might have changed
                        "       UPDATE SET testing.contentHash     = maybe.contentHash, "+
                        "                  testing.modDate         = maybe.modDate, "+
                        "                  testing.contentLocation = maybe.contentLocation, "+
                        "                  testing.status          = maybe.status, "+
                        "                  testing.lastCrawlDate   = maybe.lastCrawlDate "+
                           // But only if the new record is more recent
                        "        WHERE TO_CHAR(testing.modDate, 'YYYY-MM-DD'T'HH24:MI:SS'Z'') < TO_CHAR(maybe.modDate, ''YYYY-MM-DD'T'HH24:MI:SS'Z''') "+
                        "         WHEN NOT MATCHED THEN "+
                           // Insert new URL record
                        "   INSERT VALUES (test_seq.nextval, maybe.url, maybe.urlHash, maybe.contentHash, maybe.modDate, maybe.contentLocation, maybe.status, maybe.lastCrawlDate)");


ps.setString (1, "http://www.computergeeks.com");
          ps.setString (2, "ahsasoiowiewie");
          ps.setString (3, "sgasjwhwueybdbfndf");
          ps.setString (4, "2011-07-28T23:54:14Z");
          ps.setString (5, "c://");
          ps.setLong (6, 0);
          ps.setString (7, "2011-07-28T23:54:14Z"); 
          ps.executeUpdate();
          ps.close();
Run Code Online (Sandbox Code Playgroud)

pil*_*row 8

免责声明:我现在无法测试.你想要一个像"UPSERT"这样的东西,只有在更新的时候才能更新逻辑modDate.

在Oracle中,应该可以MERGE:

MERGE INTO testing
USING (  SELECT ? AS url,                 -- We will maybe add this record
                ? AS urlHash,
                ...
                ? AS lastCrawlDate
           FROM dual ) maybe
   ON (maybe.urlHash = testing.urlHash)
WHEN MATCHED THEN
   -- We only need update the fields that might have changed
       UPDATE SET testing.contentHash     = maybe.contentHash,
                  testing.modDate         = maybe.modDate,
                  testing.contentLocation = maybe.contentLocation,
                  testing.status          = maybe.status,
                  testing.lastCrawlDate   = maybe.lastCrawlDate
   -- But only if the new record is more recent
        WHERE testing.modDate < maybe.modDate
WHEN NOT MATCHED THEN
   -- Insert new URL record
   INSERT VALUES (test_seq.nextval, maybe.url, maybe.urlHash, ...);
Run Code Online (Sandbox Code Playgroud)

我会顺便注意,你似乎对你是缺少一些约束testing表(例如,urlurlHash看起来他们应该是唯一的,至少)

(更新:每个ruakh的评论更正)