public void batchInsertContains(int documentId, List<Integer> termIds) {
    String sqlInsert =
        "INSERT INTO contain_tbl(terms_id, documents_id, position_index)" + "VALUES(?, ?, ?)";

    try (Connection connection = Database.getConnection(mConfig);
        PreparedStatement preparedStatement = connection.prepareStatement(sqlInsert)) {
      connection.setAutoCommit(false);

      int positionId = 1;

      int i = 0;
      for (int termId : termIds) {
        preparedStatement.setInt(1, termId);
        preparedStatement.setInt(2, documentId);
        preparedStatement.setInt(3, positionId);
        preparedStatement.addBatch();
        positionId++;
        i++;
        if (i % 1000 == 0 || i == termIds.size()) {
          preparedStatement.executeBatch();
        }
      }

      connection.commit();
    } catch (SQLException ex) {
      ex.printStackTrace();
    }
  }
Beispiel #2
0
 public static boolean checkForTables(Configuration config) {
   String sqlCheck = "SHOW TABLES";
   try (Connection connection = Database.getConnection(config);
       PreparedStatement preparedStatement = connection.prepareStatement(sqlCheck);
       ResultSet res = preparedStatement.executeQuery()) {
     res.last();
     if (res.getRow() != 3) {
       return false;
     }
   } catch (SQLException e) {
     e.printStackTrace();
     return false;
   }
   return true;
 }
Beispiel #3
0
  public static boolean initDatabase(Configuration config) {
    try (Connection connection = Database.getConnection(config);
        Statement statement = connection.createStatement()) {
      Scanner s = new Scanner(FileUtil.getInputStreamFrom(PATH_TO_SQL_SCRIPT));
      s.useDelimiter("(;(\r)?\n)|(--\n)");
      while (s.hasNext()) {
        String line = s.next();
        if (line.startsWith("/*!") && line.endsWith("*/")) {
          int i = line.indexOf(' ');
          line = line.substring(i + 1, line.length() - " */".length());
        }

        if (line.trim().length() > 0) {
          statement.execute(line);
        }
      }
    } catch (SQLException | FileNotFoundException ex) {
      ex.printStackTrace();
      return false;
    }
    return true;
  }
  public static void main(String[] args) {
    Database db = new Database("exampledb.db4o");

    // Add some categories
    db.addCategory("category1", "description1", false);
    db.addCategory("category2", "description2", false);
    db.addCategory("category3", "description3", false);
    db.addCategory("deleteme", "description4", false);

    // Get stored categories
    List<String> categories = db.getCategoryNames();
    listResult(categories);

    System.out.println("--------------");

    // Delete a category
    db.deleteCategory("deleteme");
    categories = db.getCategoryNames();
    listResult(categories);

    // Construct a list of categories
    List<String> codeCategories = new ArrayList<String>();
    codeCategories.add("category1");
    codeCategories.add("category2");

    // Add a code example
    db.addCodeExample("myexample", "mydescription", "while (true)", "Java", codeCategories, false);

    System.out.println("--------------");

    // Get examples under a category name
    // Note: This only returns code titles at the moment
    List<String> examples = db.getExampleNames("category1");
    listResult(examples);

    // Get a CodeExample object from the database
    List<CodeExample> examplelist = db.getExamplesByFieldValue("title", "myexample");
    CodeExample codeexample = examplelist.get(0);

    // Get the title
    String title = codeexample.getTitle();
    System.out.println("Title: " + title);
    // Get the actual code
    String code = codeexample.getCode();
    System.out.println("Code: " + code);
    // Get the description
    String description = codeexample.getDescription();
    System.out.println("Description: " + description);
    // Get the language
    String language = codeexample.getLanguage();
    System.out.println("Language: " + language);

    // Make sure to close the database when you're done
    db.close();
  }