Skip to content

Commit

Permalink
BAEL-1419: Guide to cockroachDB in Java (eugenp#3325)
Browse files Browse the repository at this point in the history
* adding CockroachDB code

* Fixed database name

* Added handling transaction examples
  • Loading branch information
araknoid authored and pivovarit committed Jan 6, 2018
1 parent 8108875 commit fa92d1d
Show file tree
Hide file tree
Showing 6 changed files with 500 additions and 0 deletions.
2 changes: 2 additions & 0 deletions persistence-modules/java-cockroachdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Relevant Articles:
- [Guide to CockroachDB in Java](http://www.baeldung.com/)
74 changes: 74 additions & 0 deletions persistence-modules/java-cockroachdb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>

<groupId>com.baeldung</groupId>
<artifactId>java-cockroachdb</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<postgresql.version>42.1.4</postgresql.version>
</properties>

<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*ManualTest.java</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<repositories>
<repository>
<id>Central</id>
<name>Central</name>
<url>http://repo1.maven.org/maven2/</url>
<layout>default</layout>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baeldung.cockroachdb.domain;

import java.util.UUID;

public class Article {

private UUID id;

private String title;

private String author;

public Article(UUID id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.baeldung.cockroachdb.repository;

import com.baeldung.cockroachdb.domain.Article;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* Repository for the articles table related operations
*/
public class ArticleRepository {

private static final String TABLE_NAME = "articles";
private Connection connection;

public ArticleRepository(Connection connection) {
this.connection = connection;
}

/**
* Creates the articles table.
*/
public void createTable() throws SQLException {
StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(TABLE_NAME)
.append("(id uuid PRIMARY KEY, ")
.append("title string,")
.append("author string)");

final String query = sb.toString();
Statement stmt = connection.createStatement();
stmt.execute(query);
}

/**
* Alter the articles table adding a column
*
* @param columnName Column name of the additional column
* @param columnType Column type of the additional column
* @throws SQLException
*/
public void alterTable(String columnName, String columnType) throws SQLException {
StringBuilder sb = new StringBuilder("ALTER TABLE ").append(TABLE_NAME)
.append(" ADD ")
.append(columnName)
.append(" ")
.append(columnType);

final String query = sb.toString();
Statement stmt = connection.createStatement();
stmt.execute(query);
}

/**
* Insert a new article in the articles table
*
* @param article New article to insert
* @throws SQLException
*/
public void insertArticle(Article article) throws SQLException {
StringBuilder sb = new StringBuilder("INSERT INTO ").append(TABLE_NAME)
.append("(id, title, author) ")
.append("VALUES (?,?,?)");

final String query = sb.toString();
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, article.getId().toString());
preparedStatement.setString(2, article.getTitle());
preparedStatement.setString(3, article.getAuthor());
preparedStatement.execute();
}

/**
* Select article by Title
*
* @param title title of the article to retrieve
* @return article with the given title
* @throws SQLException
*/
public Article selectByTitle(String title) throws SQLException {
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME)
.append(" WHERE title = ?");

final String query = sb.toString();
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, title);

try (ResultSet rs = preparedStatement.executeQuery()) {

List<Article> articles = new ArrayList<>();

while (rs.next()) {
Article article = new Article(
UUID.fromString(rs.getString("id")),
rs.getString("title"),
rs.getString("author")
);
articles.add(article);
}
return articles.get(0);
}

}

/**
* Select all the articles
*
* @return list of all articles
* @throws SQLException
*/
public List<Article> selectAll() throws SQLException {
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME);

final String query = sb.toString();
PreparedStatement preparedStatement = connection.prepareStatement(query);
try (ResultSet rs = preparedStatement.executeQuery()) {

List<Article> articles = new ArrayList<>();

while (rs.next()) {
Article article = new Article(
UUID.fromString(rs.getString("id")),
rs.getString("title"),
rs.getString("author")
);
articles.add(article);
}
return articles;
}
}

/**
* Delete article by title
*
* @param title title of the article to delete
* @throws SQLException
*/
public void deleteArticleByTitle(String title) throws SQLException {
StringBuilder sb = new StringBuilder("DELETE FROM ").append(TABLE_NAME)
.append(" WHERE title = ?");

final String query = sb.toString();
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, title);
preparedStatement.execute();
}

/**
* Delete all rows in the table
*
* @throws SQLException
*/
public void truncateTable() throws SQLException {
StringBuilder sb = new StringBuilder("TRUNCATE TABLE ").append(TABLE_NAME);

final String query = sb.toString();
Statement stmt = connection.createStatement();
stmt.execute(query);
}

/**
* Delete table
*/
public void deleteTable() throws SQLException {
StringBuilder sb = new StringBuilder("DROP TABLE IF EXISTS ").append(TABLE_NAME);

final String query = sb.toString();
Statement stmt = connection.createStatement();
stmt.execute(query);
}
}
Loading

0 comments on commit fa92d1d

Please sign in to comment.