批处理没有运行,JDBC

时间:2018-06-18 08:50:02

标签: java database jdbc prepared-statement

我在运行批处理过程中遇到了一些问题。

首先,我还在学习JDBC等等,所以请原谅我,如果我的错误真的很愚蠢......

我必须在我的数据库中插入一些SQL查询。因此,我正在构建我的PreparedStatements:

String add = "INSERT INTO company_type VALUES (?,?)";
PreparedStatement company = write.getConnection().prepareStatement(add);

ResultSet com = types.getCompanyType();

while(com.next()) {
    System.out.print("1");
    company.setInt(1, com.getInt(1));
    company.setString(2, com.getString(2));
    company.addBatch();

    if(i > size) {
        company.executeBatch();
        i = 0;
    }
    i++;
} 

company.executeBatch();

在使用调试器后,我知道company.executeBatch();行出了问题。

它只是没有执行,似乎程序我遇到循环或静止不动。

有没有人,怎么会看到错误?

由于

P.S。我没有得到任何例外。

public Connection getConnection(){

    Connection connection = null;

    try{
        Class.forName( "oracle.jdbc.driver.OracleDriver" );
    } catch(ClassNotFoundException e) {
        this.loginmessage.setText("Fehler beim Laden des Oracle JDBC Treibers!");
        e.printStackTrace();
        return connection;
    }

    try {
        connection = DriverManager.getConnection("j******************);
    } catch (SQLException e) {
        this.loginmessage.setText("Verbindung fehlgeschlagen!");
        e.printStackTrace();
        return connection;
    }

    if (connection != null) {
        loginmessage.setText("Verbindung hergestellt!");
        return connection;
    } else {
        loginmessage.setText("Verbindung fehlgeschlagen!");
        System.out.print("huaha");
    }
    return connection;
}

2 个答案:

答案 0 :(得分:3)

您必须像这样提交交易,

write.getConnection().commit();

以下是批处理所涉及的步骤,

  1. 首先关闭autocommit,以便执行所有Batch语句 在单个事务中,并且未提交批处理中的任何操作 个别。毕竟这就是我们所需要的。
  2. 使用addBatch添加批次所需的语句。
  3. 然后通过调用来执行批处理语句 executeBatch()
  4. 最后commitroll-back交易。
  5. 下面给出了一个示例代码。

    public void saveLogEntried(List<LogEntry> entries) {
            try (Connection connObj = DriverManager.getConnection(JDBC_DB_URL, JDBC_USER, JDBC_PASS);
                    PreparedStatement prepareStatement = connObj
                            .prepareStatement("INSERT INTO logentry (date, ip, request) values(?, ?,?)")) {
                connObj.setAutoCommit(false);
                entries.forEach(entry -> {
                    try {
                        prepareStatement.setString(1, entry.getDate());
                        prepareStatement.setString(2, entry.getIp());
                        prepareStatement.setString(3, entry.getRequest());
                        prepareStatement.addBatch();
                    } catch (SQLException e) {
                        LOGGER.error("Error occurred while inserting data in to the Database", e);
                    }
                });
    
                prepareStatement.executeBatch();
                connObj.commit();
            } catch (Exception e) {
                LOGGER.error("Error occurred while inserting data in to the Database", e);
            }
        }
    

    但是不再需要Class.forName()。有关详细信息,请查看此post。您可以更改获取连接的方式并再次尝试。代码对我来说似乎有些笨拙。

答案 1 :(得分:1)

一种可能性是你没有提交,所以服务器端事务超时并回滚。