-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelDB.java
More file actions
345 lines (294 loc) · 14.6 KB
/
ExcelDB.java
File metadata and controls
345 lines (294 loc) · 14.6 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Database utility class for importing any Excel file into database
*/
public class ExcelDB {
// Database connection parameters
private static final String DB_URL = "jdbc:mysql://localhost:3306/project1";
private static final String USER = "root";
private static final String PASSWORD = "System@305";
/**
* Main method to import Excel data into database
* @param args command line arguments (optional)
*/
public static void main(String[] args) {
// Create a Scanner for user input
Scanner scanner = new Scanner(System.in);
// Default values
String excelFilePath;
String tableName;
// Prompt for Excel file path
System.out.print("Enter path to Excel file: ");
excelFilePath = scanner.nextLine().trim();
// Prompt for table name
System.out.print("Enter database table name: ");
tableName = scanner.nextLine().trim();
// Use defaults if no input provided
if (excelFilePath.isEmpty()) {
excelFilePath = "data.xlsx";
System.out.println("Using default file: " + excelFilePath);
}
if (tableName.isEmpty()) {
tableName = "excel_data";
System.out.println("Using default table: " + tableName);
}
try {
// Register the JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Using try-with-resources to ensure connection is closed automatically
try (Connection connection = DriverManager.getConnection(DB_URL, USER, PASSWORD)) {
System.out.println("Connecting to database...");
System.out.println("Connection successful!");
// Import data from Excel to Database
importExcelToDatabase(connection, excelFilePath, tableName);
System.out.println("Excel data successfully imported to database table: " + tableName);
}
} catch (ClassNotFoundException e) {
System.err.println("MySQL JDBC Driver not found!");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("Database error!");
e.printStackTrace();
} catch (IOException e) {
System.err.println("Error reading Excel file!");
e.printStackTrace();
} catch (Exception e) {
System.err.println("An unexpected error occurred!");
e.printStackTrace();
} finally {
scanner.close();
}
}
/**
* Imports data from Excel file to database by dynamically creating a table if needed
* @param connection database connection
* @param excelFilePath path to the Excel file
* @param tableName name of the table to insert data into
* @throws SQLException if a database access error occurs
* @throws IOException if an I/O error occurs
*/
private static void importExcelToDatabase(Connection connection, String excelFilePath, String tableName)
throws SQLException, IOException {
System.out.println("\nImporting data from Excel file: " + excelFilePath);
File excelFile = new File(excelFilePath);
if (!excelFile.exists()) {
throw new IOException("Excel file not found: " + excelFilePath);
}
// Create a FileInputStream object for the Excel file
try (FileInputStream fis = new FileInputStream(excelFile)) {
// Determine the type of Excel file (.xls or .xlsx)
Workbook workbook;
if (excelFilePath.toLowerCase().endsWith("xlsx")) {
workbook = new XSSFWorkbook(fis);
} else if (excelFilePath.toLowerCase().endsWith("xls")) {
workbook = new HSSFWorkbook(fis);
} else {
throw new IOException("Not a valid Excel file format. File must be .xls or .xlsx");
}
try {
// Get the first sheet from the workbook
Sheet sheet = workbook.getSheetAt(0);
// Get the first row to determine column headers
Row headerRow = sheet.getRow(0);
if (headerRow == null) {
throw new IOException("Empty Excel file or no header row found");
}
// Extract column names from header row
List<String> columnNames = new ArrayList<>();
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
Cell cell = headerRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String columnName = getCellValueAsString(cell).trim();
// If column name is empty, generate a default name
if (columnName.isEmpty()) {
columnName = "column_" + (i + 1);
}
// Clean the column name (remove spaces and special characters)
columnName = columnName.replaceAll("[^a-zA-Z0-9_]", "_").toLowerCase();
columnNames.add(columnName);
}
// Create table if it doesn't exist
createTableIfNotExists(connection, tableName, columnNames);
// Prepare SQL for inserting data
StringBuilder insertSql = new StringBuilder("INSERT INTO " + tableName + " (");
StringBuilder placeholders = new StringBuilder(") VALUES (");
for (int i = 0; i < columnNames.size(); i++) {
insertSql.append(columnNames.get(i));
placeholders.append("?");
if (i < columnNames.size() - 1) {
insertSql.append(", ");
placeholders.append(", ");
}
}
insertSql.append(placeholders).append(")");
// Insert data from Excel to database
try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql.toString())) {
int rowCount = 0;
// Start from the second row (index 1) as the first row is header
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row row = sheet.getRow(rowNum);
if (row == null) continue;
// Check if row is empty
boolean isRowEmpty = true;
for (int colNum = 0; colNum < columnNames.size(); colNum++) {
Cell cell = row.getCell(colNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (cell != null && cell.getCellType() != CellType.BLANK) {
isRowEmpty = false;
break;
}
}
if (isRowEmpty) continue;
// Process each cell in the row
for (int colNum = 0; colNum < columnNames.size(); colNum++) {
Cell cell = row.getCell(colNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
setCellValueToPreparedStatement(preparedStatement, cell, colNum + 1);
}
// Execute insert
preparedStatement.executeUpdate();
rowCount++;
// Print progress for large files
if (rowCount % 100 == 0) {
System.out.println("Processed " + rowCount + " rows...");
}
}
System.out.println("Imported " + rowCount + " rows into table " + tableName);
}
} finally {
workbook.close();
}
}
}
/**
* Creates a database table if it doesn't exist based on Excel column headers
* @param connection database connection
* @param tableName name of the table to create
* @param columnNames list of column names from Excel
* @throws SQLException if a database access error occurs
*/
private static void createTableIfNotExists(Connection connection, String tableName, List<String> columnNames)
throws SQLException {
StringBuilder createTableSql = new StringBuilder("CREATE TABLE IF NOT EXISTS " + tableName + " (");
createTableSql.append("id INT AUTO_INCREMENT PRIMARY KEY, ");
for (int i = 0; i < columnNames.size(); i++) {
createTableSql.append(columnNames.get(i)).append(" TEXT");
if (i < columnNames.size() - 1) {
createTableSql.append(", ");
}
}
createTableSql.append(")");
try (Statement statement = connection.createStatement()) {
statement.execute(createTableSql.toString());
System.out.println("Table structure verified/created: " + tableName);
System.out.println("Table has " + (columnNames.size() + 1) + " columns (including id)");
}
}
/**
* Sets cell value to prepared statement based on cell type
* @param preparedStatement prepared statement for SQL execution
* @param cell Excel cell
* @param paramIndex parameter index in prepared statement
* @throws SQLException if a database access error occurs
*/
private static void setCellValueToPreparedStatement(PreparedStatement preparedStatement, Cell cell, int paramIndex)
throws SQLException {
if (cell == null || cell.getCellType() == CellType.BLANK) {
preparedStatement.setNull(paramIndex, java.sql.Types.VARCHAR);
return;
}
switch (cell.getCellType()) {
case STRING:
preparedStatement.setString(paramIndex, cell.getStringCellValue());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
preparedStatement.setDate(paramIndex, new java.sql.Date(cell.getDateCellValue().getTime()));
} else {
preparedStatement.setDouble(paramIndex, cell.getNumericCellValue());
}
break;
case BOOLEAN:
preparedStatement.setBoolean(paramIndex, cell.getBooleanCellValue());
break;
case FORMULA:
try {
switch (cell.getCachedFormulaResultType()) {
case STRING:
preparedStatement.setString(paramIndex, cell.getStringCellValue());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
preparedStatement.setDate(paramIndex, new java.sql.Date(cell.getDateCellValue().getTime()));
} else {
preparedStatement.setDouble(paramIndex, cell.getNumericCellValue());
}
break;
case BOOLEAN:
preparedStatement.setBoolean(paramIndex, cell.getBooleanCellValue());
break;
default:
preparedStatement.setNull(paramIndex, java.sql.Types.VARCHAR);
}
} catch (Exception e) {
// Handle formula evaluation errors by using the formula string
preparedStatement.setString(paramIndex, cell.getCellFormula());
}
break;
case ERROR:
preparedStatement.setString(paramIndex, "ERROR");
break;
default:
preparedStatement.setNull(paramIndex, java.sql.Types.VARCHAR);
}
}
/**
* Utility method to get cell value as string regardless of cell type
* @param cell Excel cell
* @return String representation of cell value
*/
private static String getCellValueAsString(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toString();
} else {
// Check if it's an integer or double
double value = cell.getNumericCellValue();
if (value == Math.floor(value)) {
return String.valueOf((long)value);
}
return String.valueOf(value);
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
try {
return cell.getStringCellValue();
} catch (Exception e) {
try {
return String.valueOf(cell.getNumericCellValue());
} catch (Exception ex) {
return cell.getCellFormula();
}
}
default:
return "";
}
}
}=9o;