Skip to content

Commit a00b894

Browse files
committed
Style fixes
1 parent fc1a150 commit a00b894

File tree

1 file changed

+61
-60
lines changed
  • jdbc/basic-example/src/main/java/tech/ydb/jdbc/example

1 file changed

+61
-60
lines changed

jdbc/basic-example/src/main/java/tech/ydb/jdbc/example/Main.java

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,61 @@
1717
import org.slf4j.bridge.SLF4JBridgeHandler;
1818

1919
public class Main {
20-
private final static Logger LOG = LoggerFactory.getLogger(Main.class);
20+
private final static Logger logger = LoggerFactory.getLogger(Main.class);
21+
22+
public static void main(String[] args) {
23+
// Enable redirect Java Util Logging to SLF4J
24+
LogManager.getLogManager().reset();
25+
SLF4JBridgeHandler.install();
26+
java.util.logging.Logger.getLogger("").setLevel(Level.FINEST);
27+
28+
if (args.length != 1) {
29+
System.err.println("Usage: java -jar jdbc-basic-example.jar <connection_url>");
30+
return;
31+
}
32+
33+
String connectionUrl = args[0];
34+
35+
try (Connection connection = DriverManager.getConnection(connectionUrl)) {
36+
try {
37+
dropTable(connection);
38+
} catch (SQLException ex) {
39+
logger.warn("Can't drop table with message {}", ex.getMessage());
40+
}
41+
42+
createTable(connection);
43+
44+
simpleInsert(connection);
45+
select(connection);
46+
assertRowsCount(2, selectCount(connection));
47+
48+
batchInsert(connection);
49+
select(connection);
50+
assertRowsCount(4, selectCount(connection));
51+
52+
updateInTransaction(connection);
53+
select(connection);
54+
assertRowsCount(4, selectCount(connection));
55+
56+
deleteEmpty(connection);
57+
select(connection);
58+
assertRowsCount(2, selectCount(connection));
59+
60+
} catch (SQLException e) {
61+
logger.error("JDBC Example problem", e);
62+
}
63+
}
64+
2165
private static void dropTable(Connection connection) throws SQLException {
22-
LOG.info("Trying to drop table...");
66+
logger.info("Trying to drop table...");
2367

2468
try (Statement statement = connection.createStatement()) {
2569
statement.execute("DROP TABLE jdbc_basic_example");
2670
}
2771
}
2872

2973
private static void createTable(Connection connection) throws SQLException {
30-
LOG.info("Creating table table jdbc_basic_example");
74+
logger.info("Creating table table jdbc_basic_example");
3175

3276
try (Statement statement = connection.createStatement()) {
3377
statement.execute(""
@@ -42,19 +86,19 @@ private static void createTable(Connection connection) throws SQLException {
4286
);
4387
}
4488

45-
LOG.info("Table jdbc_basic_example was successfully created.");
89+
logger.info("Table jdbc_basic_example was successfully created.");
4690
}
4791

4892
private static long selectCount(Connection connection) throws SQLException {
4993
try (Statement statement = connection.createStatement()) {
5094
try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) AS cnt FROM jdbc_basic_example")) {
5195
if (!rs.next()) {
52-
LOG.warn("empty response");
96+
logger.warn("empty response");
5397
return 0;
5498
}
5599

56100
long rowsCount = rs.getLong("cnt");
57-
LOG.info("Table has {} rows", rowsCount);
101+
logger.info("Table has {} rows", rowsCount);
58102
return rowsCount;
59103
}
60104
}
@@ -64,19 +108,19 @@ private static void select(Connection connection) throws SQLException {
64108
try (Statement statement = connection.createStatement()) {
65109
try (ResultSet rs = statement.executeQuery("SELECT * FROM jdbc_basic_example")) {
66110
while (rs.next()) {
67-
LOG.info("read new row with id {}", rs.getInt("id"));
68-
LOG.info(" text = {}", rs.getString("c_text"));
69-
LOG.info(" instant = {}", rs.getTimestamp("c_instant"));
70-
LOG.info(" date = {}", String.valueOf(rs.getDate("c_date")));
71-
LOG.info(" bytes = {}", rs.getBytes("c_bytes"));
111+
logger.info("read new row with id {}", rs.getInt("id"));
112+
logger.info(" text = {}", rs.getString("c_text"));
113+
logger.info(" instant = {}", rs.getTimestamp("c_instant"));
114+
logger.info(" date = {}", String.valueOf(rs.getDate("c_date")));
115+
logger.info(" bytes = {}", rs.getBytes("c_bytes"));
72116
}
73117
}
74118
}
75119
}
76120

77121

78122
private static void simpleInsert(Connection connection) throws SQLException {
79-
LOG.info("Inserting 2 rows into table...");
123+
logger.info("Inserting 2 rows into table...");
80124

81125
Instant instant = Instant.parse("2023-04-03T12:30:25.000Z");
82126
byte[] byteArray = { (byte)0x00, (byte)0x23, (byte)0x45, (byte)0x98 };
@@ -101,11 +145,11 @@ private static void simpleInsert(Connection connection) throws SQLException {
101145
ps.executeUpdate();
102146
}
103147

104-
LOG.info("Rows inserted.");
148+
logger.info("Rows inserted.");
105149
}
106150

107151
private static void batchInsert(Connection connection) throws SQLException {
108-
LOG.info("Inserting 2 more rows into table...");
152+
logger.info("Inserting 2 more rows into table...");
109153

110154
Instant instant = Instant.parse("2002-02-20T13:44:55.123Z");
111155
byte[] byteArray = { (byte)0x32, (byte)0x00, (byte)0x89, (byte)0x54 };
@@ -133,11 +177,11 @@ private static void batchInsert(Connection connection) throws SQLException {
133177
ps.executeBatch();
134178
}
135179

136-
LOG.info("Rows inserted.");
180+
logger.info("Rows inserted.");
137181
}
138182

139183
private static void updateInTransaction(Connection connection) throws SQLException {
140-
LOG.info("Update some rows in transaction...");
184+
logger.info("Update some rows in transaction...");
141185

142186
connection.setAutoCommit(false);
143187

@@ -165,7 +209,7 @@ private static void updateInTransaction(Connection connection) throws SQLExcepti
165209
}
166210

167211
private static void deleteEmpty(Connection connection) throws SQLException {
168-
LOG.info("Deleting empty rows from into table...");
212+
logger.info("Deleting empty rows from into table...");
169213

170214
try (Statement statement = connection.createStatement()) {
171215
statement.execute("DELETE FROM jdbc_basic_example WHERE c_instant IS NULL");
@@ -177,47 +221,4 @@ private static void assertRowsCount(long rowsCount, long expectedRows) {
177221
throw new AssertionError("Unexpected count of rows, expected " + expectedRows + ", but got " + rowsCount);
178222
}
179223
}
180-
181-
public static void main(String[] args) {
182-
// Enable redirect Java Util Logging to SLF4J
183-
LogManager.getLogManager().reset();
184-
SLF4JBridgeHandler.install();
185-
java.util.logging.Logger.getLogger("").setLevel(Level.FINEST);
186-
187-
if (args.length != 1) {
188-
System.err.println("Usage: java -jar jdbc-basic-example.jar <connection_url>");
189-
return;
190-
}
191-
192-
String connectionUrl = args[0];
193-
194-
try (Connection connection = DriverManager.getConnection(connectionUrl)) {
195-
try {
196-
dropTable(connection);
197-
} catch (SQLException ex) {
198-
LOG.warn("Can't drop table with message {}", ex.getMessage());
199-
}
200-
201-
createTable(connection);
202-
203-
simpleInsert(connection);
204-
select(connection);
205-
assertRowsCount(2, selectCount(connection));
206-
207-
batchInsert(connection);
208-
select(connection);
209-
assertRowsCount(4, selectCount(connection));
210-
211-
updateInTransaction(connection);
212-
select(connection);
213-
assertRowsCount(4, selectCount(connection));
214-
215-
deleteEmpty(connection);
216-
select(connection);
217-
assertRowsCount(2, selectCount(connection));
218-
219-
} catch (SQLException e) {
220-
LOG.error("JDBC Example problem", e);
221-
}
222-
}
223224
}

0 commit comments

Comments
 (0)