Skip to content

Commit fa92d1d

Browse files
araknoidpivovarit
authored andcommitted
BAEL-1419: Guide to cockroachDB in Java (eugenp#3325)
* adding CockroachDB code * Fixed database name * Added handling transaction examples
1 parent 8108875 commit fa92d1d

6 files changed

Lines changed: 500 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Relevant Articles:
2+
- [Guide to CockroachDB in Java](http://www.baeldung.com/)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<artifactId>parent-modules</artifactId>
8+
<groupId>com.baeldung</groupId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<relativePath>../../</relativePath>
11+
</parent>
12+
13+
<modelVersion>4.0.0</modelVersion>
14+
15+
<groupId>com.baeldung</groupId>
16+
<artifactId>java-cockroachdb</artifactId>
17+
<version>1.0-SNAPSHOT</version>
18+
19+
<properties>
20+
<postgresql.version>42.1.4</postgresql.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.postgresql</groupId>
26+
<artifactId>postgresql</artifactId>
27+
<version>${postgresql.version}</version>
28+
</dependency>
29+
</dependencies>
30+
31+
<profiles>
32+
<profile>
33+
<id>integration</id>
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-surefire-plugin</artifactId>
39+
<executions>
40+
<execution>
41+
<phase>integration-test</phase>
42+
<goals>
43+
<goal>test</goal>
44+
</goals>
45+
<configuration>
46+
<excludes>
47+
<exclude>**/*ManualTest.java</exclude>
48+
</excludes>
49+
<includes>
50+
<include>**/*IntegrationTest.java</include>
51+
</includes>
52+
</configuration>
53+
</execution>
54+
</executions>
55+
<configuration>
56+
<systemPropertyVariables>
57+
<test.mime>json</test.mime>
58+
</systemPropertyVariables>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</profile>
64+
</profiles>
65+
66+
<repositories>
67+
<repository>
68+
<id>Central</id>
69+
<name>Central</name>
70+
<url>http://repo1.maven.org/maven2/</url>
71+
<layout>default</layout>
72+
</repository>
73+
</repositories>
74+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.cockroachdb.domain;
2+
3+
import java.util.UUID;
4+
5+
public class Article {
6+
7+
private UUID id;
8+
9+
private String title;
10+
11+
private String author;
12+
13+
public Article(UUID id, String title, String author) {
14+
this.id = id;
15+
this.title = title;
16+
this.author = author;
17+
}
18+
19+
public UUID getId() {
20+
return id;
21+
}
22+
23+
public void setId(UUID id) {
24+
this.id = id;
25+
}
26+
27+
public String getTitle() {
28+
return title;
29+
}
30+
31+
public void setTitle(String title) {
32+
this.title = title;
33+
}
34+
35+
public String getAuthor() {
36+
return author;
37+
}
38+
39+
public void setAuthor(String author) {
40+
this.author = author;
41+
}
42+
43+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.baeldung.cockroachdb.repository;
2+
3+
import com.baeldung.cockroachdb.domain.Article;
4+
5+
import java.sql.*;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.UUID;
9+
10+
/**
11+
* Repository for the articles table related operations
12+
*/
13+
public class ArticleRepository {
14+
15+
private static final String TABLE_NAME = "articles";
16+
private Connection connection;
17+
18+
public ArticleRepository(Connection connection) {
19+
this.connection = connection;
20+
}
21+
22+
/**
23+
* Creates the articles table.
24+
*/
25+
public void createTable() throws SQLException {
26+
StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(TABLE_NAME)
27+
.append("(id uuid PRIMARY KEY, ")
28+
.append("title string,")
29+
.append("author string)");
30+
31+
final String query = sb.toString();
32+
Statement stmt = connection.createStatement();
33+
stmt.execute(query);
34+
}
35+
36+
/**
37+
* Alter the articles table adding a column
38+
*
39+
* @param columnName Column name of the additional column
40+
* @param columnType Column type of the additional column
41+
* @throws SQLException
42+
*/
43+
public void alterTable(String columnName, String columnType) throws SQLException {
44+
StringBuilder sb = new StringBuilder("ALTER TABLE ").append(TABLE_NAME)
45+
.append(" ADD ")
46+
.append(columnName)
47+
.append(" ")
48+
.append(columnType);
49+
50+
final String query = sb.toString();
51+
Statement stmt = connection.createStatement();
52+
stmt.execute(query);
53+
}
54+
55+
/**
56+
* Insert a new article in the articles table
57+
*
58+
* @param article New article to insert
59+
* @throws SQLException
60+
*/
61+
public void insertArticle(Article article) throws SQLException {
62+
StringBuilder sb = new StringBuilder("INSERT INTO ").append(TABLE_NAME)
63+
.append("(id, title, author) ")
64+
.append("VALUES (?,?,?)");
65+
66+
final String query = sb.toString();
67+
PreparedStatement preparedStatement = connection.prepareStatement(query);
68+
preparedStatement.setString(1, article.getId().toString());
69+
preparedStatement.setString(2, article.getTitle());
70+
preparedStatement.setString(3, article.getAuthor());
71+
preparedStatement.execute();
72+
}
73+
74+
/**
75+
* Select article by Title
76+
*
77+
* @param title title of the article to retrieve
78+
* @return article with the given title
79+
* @throws SQLException
80+
*/
81+
public Article selectByTitle(String title) throws SQLException {
82+
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME)
83+
.append(" WHERE title = ?");
84+
85+
final String query = sb.toString();
86+
PreparedStatement preparedStatement = connection.prepareStatement(query);
87+
preparedStatement.setString(1, title);
88+
89+
try (ResultSet rs = preparedStatement.executeQuery()) {
90+
91+
List<Article> articles = new ArrayList<>();
92+
93+
while (rs.next()) {
94+
Article article = new Article(
95+
UUID.fromString(rs.getString("id")),
96+
rs.getString("title"),
97+
rs.getString("author")
98+
);
99+
articles.add(article);
100+
}
101+
return articles.get(0);
102+
}
103+
104+
}
105+
106+
/**
107+
* Select all the articles
108+
*
109+
* @return list of all articles
110+
* @throws SQLException
111+
*/
112+
public List<Article> selectAll() throws SQLException {
113+
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME);
114+
115+
final String query = sb.toString();
116+
PreparedStatement preparedStatement = connection.prepareStatement(query);
117+
try (ResultSet rs = preparedStatement.executeQuery()) {
118+
119+
List<Article> articles = new ArrayList<>();
120+
121+
while (rs.next()) {
122+
Article article = new Article(
123+
UUID.fromString(rs.getString("id")),
124+
rs.getString("title"),
125+
rs.getString("author")
126+
);
127+
articles.add(article);
128+
}
129+
return articles;
130+
}
131+
}
132+
133+
/**
134+
* Delete article by title
135+
*
136+
* @param title title of the article to delete
137+
* @throws SQLException
138+
*/
139+
public void deleteArticleByTitle(String title) throws SQLException {
140+
StringBuilder sb = new StringBuilder("DELETE FROM ").append(TABLE_NAME)
141+
.append(" WHERE title = ?");
142+
143+
final String query = sb.toString();
144+
PreparedStatement preparedStatement = connection.prepareStatement(query);
145+
preparedStatement.setString(1, title);
146+
preparedStatement.execute();
147+
}
148+
149+
/**
150+
* Delete all rows in the table
151+
*
152+
* @throws SQLException
153+
*/
154+
public void truncateTable() throws SQLException {
155+
StringBuilder sb = new StringBuilder("TRUNCATE TABLE ").append(TABLE_NAME);
156+
157+
final String query = sb.toString();
158+
Statement stmt = connection.createStatement();
159+
stmt.execute(query);
160+
}
161+
162+
/**
163+
* Delete table
164+
*/
165+
public void deleteTable() throws SQLException {
166+
StringBuilder sb = new StringBuilder("DROP TABLE IF EXISTS ").append(TABLE_NAME);
167+
168+
final String query = sb.toString();
169+
Statement stmt = connection.createStatement();
170+
stmt.execute(query);
171+
}
172+
}

0 commit comments

Comments
 (0)