Exemple #1
0
  public Connection executeBatch() throws Sql2oException {
    long start = System.currentTimeMillis();
    try {
      logExecution();
      PreparedStatement statement = buildPreparedStatement();
      connection.setBatchResult(statement.executeBatch());
      this.currentBatchRecords = 0;
      try {
        connection.setKeys(this.returnGeneratedKeys ? statement.getGeneratedKeys() : null);
        connection.setCanGetKeys(this.returnGeneratedKeys);
      } catch (SQLException sqlex) {
        throw new Sql2oException(
            "Error while trying to fetch generated keys from database. If you are not expecting any generated keys, fix this error by setting the fetchGeneratedKeys parameter in the createQuery() method to 'false'",
            sqlex);
      }
    } catch (Throwable e) {
      this.connection.onException();
      throw new Sql2oException("Error while executing batch operation: " + e.getMessage(), e);
    } finally {
      closeConnectionIfNecessary();
    }

    long end = System.currentTimeMillis();
    logger.debug(
        "total: {} ms; executed batch [{}]",
        new Object[] {end - start, this.getName() == null ? "No name" : this.getName()});

    return this.connection;
  }
  int create_war(
      Channel channel,
      String starter,
      String name,
      long base_duration,
      long duration,
      long remaining,
      long time_to_start,
      int total_chains,
      int current_chain,
      int break_duration,
      boolean do_randomness) {
    int id = 0;
    Connection con = null;
    try {
      con = pool.getConnection(timeout);

      PreparedStatement s =
          con.prepareStatement(
              "INSERT INTO `wars` (`channel`, `starter`, `name`, `base_duration`, `randomness`, `delay`, `duration`, `remaining`, `time_to_start`, `total_chains`, `current_chain`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
              Statement.RETURN_GENERATED_KEYS);
      s.setString(1, channel.getName().toLowerCase());
      s.setString(2, starter);
      s.setString(3, name);
      s.setLong(4, base_duration);
      s.setBoolean(5, do_randomness);
      s.setLong(6, break_duration);
      s.setLong(7, duration);
      s.setLong(8, remaining);
      s.setLong(9, time_to_start);
      s.setInt(10, total_chains);
      s.setInt(11, current_chain);
      s.executeUpdate();

      ResultSet rs = s.getGeneratedKeys();

      if (rs.next()) {
        id = rs.getInt(1);
      }

      rs.close();
      s.close();
    } catch (SQLException ex) {
      Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        if (con != null) {
          con.close();
        }
      } catch (SQLException ex) {
        Logger.getLogger(DBAccess.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    return id;
  }
  public boolean insert(Local_dept local_dept) throws SQLException {

    PreparedStatement stmt =
        conexao.prepareStatement(
            "insert into local_dept (num_dept,cod_local) values (?, ?) ",
            Statement.RETURN_GENERATED_KEYS);
    stmt.setInt(1, local_dept.getNum_dept());
    stmt.setInt(2, local_dept.getCod_local());
    int linhas = stmt.executeUpdate();

    ResultSet rs = stmt.getGeneratedKeys();
    if (rs != null && rs.next()) {
      local_dept.setNum_dept(rs.getInt(1));
    }

    rs.close();
    stmt.close();
    return linhas > 0;
  }
  public static int insertWithGeneratedKey(String query) {
    int lastInsertId = 0;
    try {
      Class.forName("com.mysql.jdbc.Driver");
      String dbpwd = Utilfunctions.getDbConfig("password");
      if (con == null)
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/schedulepro", "root", dbpwd);

      PreparedStatement statement = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
      statement.executeUpdate();

      ResultSet keys = statement.getGeneratedKeys();
      keys.next();
      lastInsertId = keys.getInt(1);

      return lastInsertId;
    } catch (Exception e) {
      JOptionPane.showMessageDialog(null, e);
    }
    return lastInsertId;
  }
Exemple #5
0
  public Connection executeUpdate() {
    long start = System.currentTimeMillis();
    try {
      logExecution();
      PreparedStatement statement = buildPreparedStatement();
      this.connection.setResult(statement.executeUpdate());
      this.connection.setKeys(this.returnGeneratedKeys ? statement.getGeneratedKeys() : null);
      connection.setCanGetKeys(this.returnGeneratedKeys);
    } catch (SQLException ex) {
      this.connection.onException();
      throw new Sql2oException("Error in executeUpdate, " + ex.getMessage(), ex);
    } finally {
      closeConnectionIfNecessary();
    }

    long end = System.currentTimeMillis();
    logger.debug(
        "total: {} ms; executed update [{}]",
        new Object[] {end - start, this.getName() == null ? "No name" : this.getName()});

    return this.connection;
  }