-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_excel_insert_database.java
127 lines (101 loc) · 4.13 KB
/
read_excel_insert_database.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
package excel_read;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import static java.lang.Integer.parseInt;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.sql.*;
/**
*
* @author furkantekke
*/
public class Excel_read {
/**
* @param args the command line arguments
*/
// JDBC driver and Database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/deneme";
// database user name and password
static final String USER = "root";
static final String PASS = "pass";
public static void main(String[] args) throws FileNotFoundException, IOException {
Connection conn = null;
PreparedStatement stmt =null;
try{
Class.forName("com.mysql.jdbc.Driver");
// Baglantı acılır.
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//bağlantı başarılıysa devam eder
String query = " insert into database_table_name (soyad,adres,enlembilgisi,boylambilgisi,postakod,ilcesemt,sehir,ulke)"
+ " values (?,?,?,?,?,?,?,?)";
stmt = conn.prepareStatement(query);
String okunacak = "/Users/furkantekke/Desktop/Mission 3/okunacak_excel.xlsx";
FileInputStream fil = new FileInputStream(new File(okunacak));
Workbook wb = WorkbookFactory.create(fil);
Sheet oku = wb.getSheetAt(0);
int a=1; //for use column names in prepared statement query
//for reading datas from excel and insert them into database
// 98 data are in my excel sheet
for(int j=1;j<=97;j++){
for(int i=4;i<=11;i++){
Cell cell = oku.getRow(j).getCell(i); //choosing data to readable
String x = getCellValueAsString(cell); //choosed data is converting string with getCellValueAsString
if(i==9)//if readed data is in column 9 than insert database 1
stmt.setInt(a, 1);
else if(i==11){//if readed data is in 11 column than convert it to integer value than insert in database
int result = Integer.parseInt(x);
stmt.setInt(a,result);}
else//all other situations are inserting database
stmt.setString (a, x);
a++;//selecting next column in query
}
a=1;//refresh query columns
stmt.execute(); //rows are inserted database and saved
}
conn.close();//close the database connection
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
System.out.println(e);
}
}
public static String getCellValueAsString(Cell cell) {
String strCellValue = null;
if (cell != null) {
switch (cell.getCellType()) {
case STRING:
strCellValue = cell.toString();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy");
strCellValue = dateFormat.format(cell.getDateCellValue());
} else {
Double value = cell.getNumericCellValue();
Long longValue = value.longValue();
strCellValue = new String(longValue.toString());
}
break;
case BLANK:
strCellValue = "";
break;
}
}
return strCellValue;
}
}