1
- /* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.*/
1
+ /* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2
+ Licensed under the Universal Permissive License v 1.0
3
+ as shown at http://oss.oracle.com/licenses/upl
4
+ */
2
5
3
6
/*
4
7
DESCRIPTION
5
- The code sample connects to the Oracle Database and creates a table 'todoitem'.
8
+ The code sample connects to the Oracle Database and creates a table 'todoitem'.
9
+ It inserts few records into this table and displays the list of tasks and task completion status.
6
10
Edit this file and update the connection URL along with the database username and password
7
11
that point to your database.
8
12
@@ -27,18 +31,19 @@ public class QuickStart {
27
31
// Change this URL to match your target Oracle database (XE or else)
28
32
final static String DB_URL ="jdbc:oracle:thin:@//localhost:1521/XEPDB1" ;
29
33
// Enter the database user
30
- final static String DB_USER = "jdbctest " ;
34
+ final static String DB_USER = "<db-user> " ;
31
35
// Enter the database password
32
- final static String DB_PASSWORD = "jdbctest " ;
36
+ final static String DB_PASSWORD = "<db-password> " ;
33
37
final static String CONN_FACTORY_CLASS_NAME ="oracle.jdbc.pool.OracleDataSource" ;
34
38
35
39
/*
36
- * The sample demonstrates UCP as client side connection pool.
40
+ * The sample creates a table 'todoitem' that lists tasks and task completion status.
41
+ * Requirement: database connection string, database user and database password
37
42
*/
38
43
public static void main (String args []) throws Exception {
44
+
39
45
// Get the PoolDataSource for UCP
40
46
PoolDataSource pds = PoolDataSourceFactory .getPoolDataSource ();
41
-
42
47
// Set the connection factory
43
48
pds .setConnectionFactoryClassName (CONN_FACTORY_CLASS_NAME );
44
49
pds .setURL (DB_URL );
@@ -59,7 +64,6 @@ public static void main(String args[]) throws Exception {
59
64
+ pds .getAvailableConnectionsCount ());
60
65
System .out .println ("Borrowed connections after checkout: "
61
66
+ pds .getBorrowedConnectionsCount ());
62
- // Perform a database operation
63
67
doSQLWork (conn );
64
68
}
65
69
@@ -70,30 +74,30 @@ public static void main(String args[]) throws Exception {
70
74
}
71
75
72
76
/*
73
- * Creates a todoitem table and insert few rows, and select operations on
74
- * the new table created. Remove the table after verifying the data.
77
+ * Creates a ' todoitem' table, insert few rows, and select the data from
78
+ * the table created. Remove the table after verifying the data.
75
79
*/
76
80
public static void doSQLWork (Connection conn ) {
77
81
try {
78
82
conn .setAutoCommit (false );
79
- // Prepare a statement to execute the SQL Queries .
83
+ // Prepare a statement to execute the SQL Statement .
80
84
Statement statement = conn .createStatement ();
81
85
86
+ // Create a table 'todoitem'
82
87
String createSQL = "CREATE TABLE todoitem "
83
88
+ "(id NUMBER GENERATED ALWAYS AS IDENTITY,"
84
89
+ " description VARCHAR2(4000), "
85
90
+ " creation_ts TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,"
86
91
+ " done NUMBER(1, 0), PRIMARY KEY(id))" ;
87
-
88
- // Create a table "todoitem"
92
+
89
93
statement .executeUpdate (createSQL );
90
- System .out .println ("New table todoitem is created" );
94
+ System .out .println ("New table ' todoitem' is created" );
91
95
92
96
//Insert sample data
93
97
String [] description = { "Task 1" , "Task 2" , "Task 3" , "Task 4" , "Task 5" };
94
98
int [] done = { 0 , 0 , 1 , 0 , 1 };
95
99
96
- // Insert some records into the table CUSTOMER
100
+ // Insert some records into the table 'todoitem'
97
101
PreparedStatement prepStatement = conn .prepareStatement ("INSERT INTO "
98
102
+ " todoitem (description, done) VALUES(?, ?)" );
99
103
for (int row = 0 ; row < description .length ; row ++) {
@@ -103,9 +107,9 @@ public static void doSQLWork(Connection conn) {
103
107
}
104
108
prepStatement .executeBatch ();
105
109
106
- System .out .println ("Two records are inserted. " );
110
+ System .out .println ("New records are inserted" );
107
111
108
- // Verify the table "todoitem"
112
+ // Verify the data from the table "todoitem"
109
113
ResultSet resultSet = statement .executeQuery ("SELECT DESCRIPTION, DONE FROM TODOITEM" );
110
114
System .out .println ("\n New table 'todoitem' contains:" );
111
115
System .out .println ("DESCRIPTION" + "\t " + "DONE" );
0 commit comments