-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListTallPackages.java
192 lines (167 loc) · 8.45 KB
/
ListTallPackages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ListTallPackages {
// JDBC URL for connecting to the DB2 database
private static final String DB_URL = "jdbc:db2://localhost:50000/testdb";
// Database username
private static final String USER = "db2inst1";
// Database password
private static final String PASSWORD = "password";
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
// Load the DB2 JDBC driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("DB2 JDBC Driver loaded successfully.");
// Establish a connection to the database
connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
System.out.println("Connected to the database successfully.");
// 1. Delete all entries in the Package table
System.out.println("Deleting all entries in the Package table...");
String sqlDelete = "DELETE FROM Package";
preparedStatement = connection.prepareStatement(sqlDelete);
int rowsDeleted = preparedStatement.executeUpdate();
System.out.println(rowsDeleted + " rows deleted.");
// 2. Insert specific entries into the Package table
System.out.println("Inserting new entries into the Package table...");
String sqlInsert = "INSERT INTO Package (id, height, length, width, description) VALUES (?, ?, ?, ?, ?)";
preparedStatement = connection.prepareStatement(sqlInsert);
// Insert data for each package
addPackage(preparedStatement, 70071, 17.0f, 17.1f, 7.7f, "Should be Second");
addPackage(preparedStatement, 70077, 77.0f, 17.7f, 7.7f, "Should be Third");
addPackage(preparedStatement, 70007, 70.0f, 10.7f, 0.7f, "Should not be returned");
addPackage(preparedStatement, 70073, 77.0f, 17.7f, 7.8f, "Should be First");
addPackage(preparedStatement, 70076, 104.0f, 17.7f, 7.7f, "Should be Fourth");
// Execute batch insert
preparedStatement.executeBatch();
// 3. List all packages
System.out.println("\nListing all packages...");
String sqlAll = "SELECT * FROM Package";
preparedStatement = connection.prepareStatement(sqlAll);
resultSet = preparedStatement.executeQuery();
List<Package> allPackages = new ArrayList<>();
while (resultSet.next()) {
int id = resultSet.getInt("id");
float height = resultSet.getFloat("height");
float length = resultSet.getFloat("length");
float width = resultSet.getFloat("width");
String description = resultSet.getString("description");
Package pkg = Package.of(id, height, length);
pkg.setWidth(width);
pkg.setDescription(description);
allPackages.add(pkg);
}
// Print all packages
System.out.println("ID | Height | Length | Width | Description");
System.out.println("---|--------|--------|-------|-------------");
for (Package pkg : allPackages) {
System.out.println("id :" + pkg.getId()+" height :"+pkg.getHeight()+"description:"+pkg.getDescription());
}
// 4. Retrieve and print ordered packages
// System.out.println("\nRetrieving ordered packages...");
// String sqlOrdered = "SELECT * FROM Package WHERE height < ? ORDER BY height DESC, length";
// preparedStatement = connection.prepareStatement(sqlOrdered);
// preparedStatement.setDouble(1, 8.0); // Example parameter
// preparedStatement.setMaxRows(4);
// resultSet = preparedStatement.executeQuery();
// List<Package> orderedPackages = new ArrayList<>();
// while (resultSet.next()) {
// int id = resultSet.getInt("id");
// float height = resultSet.getFloat("height");
// float length = resultSet.getFloat("length");
// float width = resultSet.getFloat("width");
// String description = resultSet.getString("description");
// Package pkg = Package.of(id, height, length);
// pkg.setWidth(width);
// pkg.setDescription(description);
// orderedPackages.add(pkg);
// }
// // Print ordered packages
// System.out.println("\nID | Height | Length | Width | Description");
// System.out.println("---|--------|--------|-------|-------------");
// for (Package pkg : orderedPackages) {
// System.out.println("id :" + pkg.getId()+" height :"+pkg.getHeight()+"description:"+pkg.getDescription());
// }
System.out.println("\nRetrieving ordered packages with row limits...");
// Modified SQL query with row limits and locks
String sqlComplexOrdered =
"SELECT * FROM ( " +
" SELECT * FROM ( " +
" SELECT EL_TEMP.*, ROWNUMBER() OVER() AS EL_ROWNM " +
" FROM ( " +
" SELECT ID AS a1, DESCRIPTION AS a2, HEIGHT AS a3, LENGTH AS a4, WIDTH AS a5 " +
" FROM PACKAGE " +
" WHERE HEIGHT < ? " +
" ORDER BY HEIGHT DESC, LENGTH " +
" ) AS EL_TEMP " +
" ) AS EL_TEMP2 WHERE EL_ROWNM <= ? " +
") AS EL_TEMP3 WHERE EL_ROWNM > ? " +
"FOR READ ONLY WITH RS USE AND KEEP UPDATE LOCKS";
preparedStatement = connection.prepareStatement(sqlComplexOrdered);
// Set the bind parameters
preparedStatement.setDouble(1, 8.0); // Example parameter for HEIGHT < 8.0
preparedStatement.setInt(2, 2); // Upper limit for ROWNUM (EL_ROWNM <= 2)
preparedStatement.setInt(3, 0); // Lower limit for ROWNUM (EL_ROWNM > 0)
preparedStatement.setMaxRows(4); // Optional, limiting the total number of rows returned
resultSet = preparedStatement.executeQuery();
// Processing the result set
while (resultSet.next()) {
int id = resultSet.getInt("a1");
String description = resultSet.getString("a2");
float height = resultSet.getFloat("a3");
float length = resultSet.getFloat("a4");
float width = resultSet.getFloat("a5");
System.out.println("ID: " + id + ", Description: " + description +
", Height: " + height + ", Length: " + length +
", Width: " + width);
}
} catch (ClassNotFoundException e) {
System.err.println("DB2 JDBC Driver not found. Include the driver in your classpath.");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("SQL exception occurred while connecting to or querying the DB2 database.");
e.printStackTrace();
} finally {
// Ensure resources are closed properly
try {
if (resultSet != null && !resultSet.isClosed()) {
resultSet.close();
}
} catch (SQLException e) {
System.err.println("Failed to close the result set.");
e.printStackTrace();
}
try {
if (preparedStatement != null && !preparedStatement.isClosed()) {
preparedStatement.close();
}
} catch (SQLException e) {
System.err.println("Failed to close the prepared statement.");
e.printStackTrace();
}
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
System.err.println("Failed to close the database connection.");
e.printStackTrace();
}
}
}
private static void addPackage(PreparedStatement preparedStatement, int id,float length,float width, float height,String description) throws SQLException {
preparedStatement.setInt(1, id);
preparedStatement.setFloat(2, height);
preparedStatement.setFloat(3, length);
preparedStatement.setFloat(4, width);
preparedStatement.setString(5, description);
preparedStatement.addBatch();
}
}