17
17
import org .slf4j .bridge .SLF4JBridgeHandler ;
18
18
19
19
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
+
21
65
private static void dropTable (Connection connection ) throws SQLException {
22
- LOG .info ("Trying to drop table..." );
66
+ logger .info ("Trying to drop table..." );
23
67
24
68
try (Statement statement = connection .createStatement ()) {
25
69
statement .execute ("DROP TABLE jdbc_basic_example" );
26
70
}
27
71
}
28
72
29
73
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" );
31
75
32
76
try (Statement statement = connection .createStatement ()) {
33
77
statement .execute (""
@@ -42,19 +86,19 @@ private static void createTable(Connection connection) throws SQLException {
42
86
);
43
87
}
44
88
45
- LOG .info ("Table jdbc_basic_example was successfully created." );
89
+ logger .info ("Table jdbc_basic_example was successfully created." );
46
90
}
47
91
48
92
private static long selectCount (Connection connection ) throws SQLException {
49
93
try (Statement statement = connection .createStatement ()) {
50
94
try (ResultSet rs = statement .executeQuery ("SELECT COUNT(*) AS cnt FROM jdbc_basic_example" )) {
51
95
if (!rs .next ()) {
52
- LOG .warn ("empty response" );
96
+ logger .warn ("empty response" );
53
97
return 0 ;
54
98
}
55
99
56
100
long rowsCount = rs .getLong ("cnt" );
57
- LOG .info ("Table has {} rows" , rowsCount );
101
+ logger .info ("Table has {} rows" , rowsCount );
58
102
return rowsCount ;
59
103
}
60
104
}
@@ -64,19 +108,19 @@ private static void select(Connection connection) throws SQLException {
64
108
try (Statement statement = connection .createStatement ()) {
65
109
try (ResultSet rs = statement .executeQuery ("SELECT * FROM jdbc_basic_example" )) {
66
110
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" ));
72
116
}
73
117
}
74
118
}
75
119
}
76
120
77
121
78
122
private static void simpleInsert (Connection connection ) throws SQLException {
79
- LOG .info ("Inserting 2 rows into table..." );
123
+ logger .info ("Inserting 2 rows into table..." );
80
124
81
125
Instant instant = Instant .parse ("2023-04-03T12:30:25.000Z" );
82
126
byte [] byteArray = { (byte )0x00 , (byte )0x23 , (byte )0x45 , (byte )0x98 };
@@ -101,11 +145,11 @@ private static void simpleInsert(Connection connection) throws SQLException {
101
145
ps .executeUpdate ();
102
146
}
103
147
104
- LOG .info ("Rows inserted." );
148
+ logger .info ("Rows inserted." );
105
149
}
106
150
107
151
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..." );
109
153
110
154
Instant instant = Instant .parse ("2002-02-20T13:44:55.123Z" );
111
155
byte [] byteArray = { (byte )0x32 , (byte )0x00 , (byte )0x89 , (byte )0x54 };
@@ -133,11 +177,11 @@ private static void batchInsert(Connection connection) throws SQLException {
133
177
ps .executeBatch ();
134
178
}
135
179
136
- LOG .info ("Rows inserted." );
180
+ logger .info ("Rows inserted." );
137
181
}
138
182
139
183
private static void updateInTransaction (Connection connection ) throws SQLException {
140
- LOG .info ("Update some rows in transaction..." );
184
+ logger .info ("Update some rows in transaction..." );
141
185
142
186
connection .setAutoCommit (false );
143
187
@@ -165,7 +209,7 @@ private static void updateInTransaction(Connection connection) throws SQLExcepti
165
209
}
166
210
167
211
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..." );
169
213
170
214
try (Statement statement = connection .createStatement ()) {
171
215
statement .execute ("DELETE FROM jdbc_basic_example WHERE c_instant IS NULL" );
@@ -177,47 +221,4 @@ private static void assertRowsCount(long rowsCount, long expectedRows) {
177
221
throw new AssertionError ("Unexpected count of rows, expected " + expectedRows + ", but got " + rowsCount );
178
222
}
179
223
}
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
- }
223
224
}
0 commit comments