forked from manishppp2530/ExceltoMysql_Using-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadExcelIntoMySQL
More file actions
44 lines (38 loc) · 2.18 KB
/
ReadExcelIntoMySQL
File metadata and controls
44 lines (38 loc) · 2.18 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
try {
System.out.println("Getting into FileUploadtoDB");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection) DriverManager.getConnection(url,username,password);
con.setAutoCommit(false);
PreparedStatement pstm = null;
FileInputStream input = new FileInputStream(filepath);
// InputStream inp = new FileInputStream(fname);
System.out.println("Checking path : " + input);
// XSSFWorkbook wb = new XSSFWorkbook(filepath); // Declare XSSF WorkBook
XSSFWorkbook wb1 = new XSSFWorkbook(input);
Sheet sheet = wb1.getSheetAt(0);
System.out.println("Sheet : " + sheet );
// System.out.println("sheet " + sheet);
XSSFRow row = null;
System.out.println("row " + row);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
System.out.println("Iteration starts here");
row = (XSSFRow) sheet.getRow(i);
System.out.println("row 2 : " + row);
String Process = row.getCell(1).getStringCellValue();
System.out.println("Process " + Process);
String Level = row.getCell(2).getStringCellValue();
String A = row.getCell(3).getStringCellValue();
String B = row.getCell(4).getStringCellValue();
String C = row.getCell(5).getStringCellValue();
System.out.println("Process " + Process );
String sql = "insert into exceldata(Process, Level, A, B, C) values(?, ?, ?, ?, ?)";
System.out.println("Sql query : " + sql);
pstm = (PreparedStatement) con.prepareStatement(sql);
pstm.setString(1, Process);
pstm.setString(2, Level);
pstm.setString(3, A);
pstm.setString(4, B);
pstm.setString(5, C);
pstm.execute();
System.out.println("Import rows " + i);
}