Skip to content

Commit e732fa5

Browse files
committed
updated the JDBC demo program
1 parent ceb55d1 commit e732fa5

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

src/jdbc/apps/jdbc_demo_app/src/App.java

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,19 @@ public static void main(String[] args) throws Exception {
99
final String username = "root";
1010
final String password = "password";
1111

12-
/* Establishing a connection with the DB */
12+
System.out.println("Establishing a connection to the database...\n");
1313
try (Connection connection = DriverManager.getConnection(url, username, password)) {
1414
/* Fetching some DB metadata */
1515
System.out.println("Connected to database server "
1616
+ connection.getMetaData().getDatabaseProductName()
1717
+ " version: "
18-
+ connection.getMetaData().getDatabaseProductVersion());
18+
+ connection.getMetaData().getDatabaseProductVersion()
19+
+ "\n");
1920

20-
/* Statement Demo */
21+
System.out.println("USING STATEMENT:: ");
2122
try (Statement statement = connection.createStatement()) {
22-
// Read (Retrieve) Operation using Statement
23-
String sql = "SELECT * FROM Worker WHERE WORKER_ID = 3";
24-
ResultSet result = statement.executeQuery(sql);
25-
while (result.next()) {
26-
int id = result.getInt("WORKER_ID");
27-
String firstName = result.getString(1);
28-
System.out.println(id + "\t" + firstName);
29-
}
3023

31-
// Create Operation using Statement
24+
System.out.println("Create operation:");
3225
String insertSql = """
3326
INSERT INTO Worker (
3427
WORKER_ID,
@@ -39,31 +32,63 @@ INSERT INTO Worker (
3932
DEPARTMENT)
4033
VALUES (
4134
10, 'Tony', 'Stark', '50000000', NOW(), 'R&D'
42-
)""";
35+
)
36+
""";
4337
int rowsInserted = statement.executeUpdate(insertSql);
4438
System.out.println(rowsInserted + " rows inserted.");
39+
System.out.println();
4540

46-
// Update Operation using Statement
41+
System.out.println("Read//Retrieve operation:");
42+
String selectAllSql = """
43+
SELECT
44+
*
45+
FROM
46+
Worker
47+
""";
48+
ResultSet result = statement.executeQuery(selectAllSql);
49+
System.out.println("WORKER_ID\tFIRST_NAME\tLAST_NAME\tSALARY\tJOINING_DATE\tDEPARTMENT");
50+
while (result.next()) {
51+
int workerId = result.getInt("WORKER_ID");
52+
String firstName = result.getString("FIRST_NAME");
53+
String lastName = result.getString("LAST_NAME");
54+
int salary = result.getInt(4);
55+
Timestamp joiningDate = result.getTimestamp("JOINING_DATE");
56+
String department = result.getString(6);
57+
System.out.println(
58+
String.format(
59+
"%d\t%s\t%s\t%d\t%s\t%s",
60+
workerId,
61+
firstName,
62+
lastName,
63+
salary,
64+
joiningDate,
65+
department));
66+
}
67+
System.out.println();
68+
69+
System.out.println("Update operation:");
4770
String updateSql1 = """
4871
UPDATE Worker
4972
SET
5073
FIRST_NAME = 'Iron'
5174
WHERE
52-
WORKER_ID = 10""";
75+
FIRST_NAME = 'Tony'""";
5376
int rowsUpdated = statement.executeUpdate(updateSql1);
5477
System.out.println(rowsUpdated + " rows updated.");
78+
System.out.println();
5579

56-
// Batch Update using Statement
80+
System.out.println("Batch Update operation:");
5781
String updateSql2 = """
5882
UPDATE Worker
5983
SET
6084
LAST_NAME = 'Man'
6185
WHERE
62-
WORKER_ID = 10""";
86+
LAST_NAME = 'Stark'""";
6387
statement.addBatch(updateSql1);
6488
statement.addBatch(updateSql2);
6589
int[] recordsAffected = statement.executeBatch();
6690
System.out.println("Status of batch updates:: " + Arrays.toString(recordsAffected));
91+
System.out.println();
6792

6893
// Delete Operation using Statement
6994
String deleteSql = """
@@ -72,10 +97,11 @@ INSERT INTO Worker (
7297
WORKER_ID = 10""";
7398
int rowsDeleted = statement.executeUpdate(deleteSql);
7499
System.out.println(rowsDeleted + " rows deleted.");
100+
System.out.println();
75101
}
76102

77-
/* PreparedStatement Demo */
78-
// Read (Retrieve) Operation using PreparedStatement
103+
System.out.println("USING PREPARED STATEMENT:: ");
104+
System.out.println("Read//Retrieve operation:");
79105
String selectFormat = """
80106
SELECT *
81107
FROM
363 KB
Loading

0 commit comments

Comments
 (0)