JDBC executeQuery和updateRow不适用于特定的表

时间:2016-11-18 09:00:29

标签: java oracle jdbc

我有以下代码:

package sql_updates;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.lang3.RandomStringUtils;


public class Sql_updates {


    public static void main(String[] args) throws SQLException, ClassNotFoundException {

        int gen_p = 1600;
        int gl_p = 400;

        Class.forName ("oracle.jdbc.driver.OracleDriver");
        String url = "jdbc:oracle:thin:@lbuchpsooraswat:1521:AXATEST";

        //properties for creating connection to Oracle database
        Properties props = new Properties();
        props.setProperty("user", "COMPOSER");
        props.setProperty("password", "COMPOSER");

        //creating connection to Oracle database using JDBC
        Connection con = DriverManager.getConnection(url, props);

        Statement stmt = null;

        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        HashSet hs = new HashSet();

        for (int i = 1; i <= gen_p; i++) {
            hs.add("#" + RandomStringUtils.random(7, true, true));

        }

        for (int i = 1; i <= gl_p; i++) {
            hs.add(RandomStringUtils.random(7, true, true));

        }

        ResultSet uprs = stmt.executeQuery(
                "SELECT ACC FROM table_p");

        Iterator<String> it = hs.iterator();
        int i = 0;
        while (uprs.next() && it.hasNext()) {

            String f = uprs.getString("ACC");

            uprs.updateString("ACC", it.next());
            uprs.updateRow();
            i++;
        }

        System.out.println("am terminat update-ul");

        stmt.executeUpdate(
                "INSERT INTO TABLE_G(TC, GACC, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, START_DATE, END_DATE) SELECT TC, ACC, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, START_DATE, END_DATE  FROM TABLE_P WHERE ACC LIKE '#%'");

stmt.close();
        System.out.println("am terminat INSERT-ul");
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);


        System.out.println("am terminat UPDATE-ul");


        HashSet hs2 = new HashSet();

        for (int j = 1; j <= 2000; j++) {
            hs2.add(RandomStringUtils.random(7, true, true));

        }

        Iterator<String> it2 = hs2.iterator();
        System.out.println("incep update-ul");
        uprs = stmt.executeQuery("SELECT ACC FROM table_g");


        int zz=1;
        while (uprs.next()) {
            String f = uprs.getString("ACC");
            uprs.updateString("ACC", RandomStringUtils.random(7, true, true));
            uprs.updateRow();
            zz++;
            if(zz%100==0)
            System.out.println("aaaaa   "+zz);
        }

        stmt.close();
        con.close();

        System.out.println("am terminat update-ul");
    }

}

我的问题是,即使我的table_g中的第一个插入操作正常,updateRow部分也会被阻止而没有任何错误消息,我不明白为什么......我无法理解的是有时候它停在1600左右,或4500左右,或者有时它不会打印任何东西,但它会卡住,什么也不做。我认为它可能与我的table_g有关,但为什么插入工作呢?更重要的是,当我直接尝试stmt.executeUpdate(“UPDATE TABLE_G SET ACC ='X'”);同样的事情,它是冻结的。你能帮我吗?

谢谢!

P.S。:对于无组织的代码抱歉:)

0 个答案:

没有答案