Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestApp {
- public static void main(String[] args) throws Exception {
- try {
- Class forName = Class.forName("com.mysql.jdbc.Driver");
- Connection con = null;
- con =
- DriverManager.getConnection("jdbc:mysql://192.168.1.13:3307/db1",
- "sachin-LH", "sachin@123");
- con.setAutoCommit(false);
- PreparedStatement pstm = null;
- FileInputStream input = new
- FileInputStream("C:\Users\lh\Downloads\Student Details(2009-
- 13).xls");
- POIFSFileSystem fs = new POIFSFileSystem(input);
- Workbook workbook;
- workbook = WorkbookFactory.create(fs);
- Sheet sheet = workbook.getSheetAt(0);
- Row row;
- for (int i = 1; i <= sheet.getLastRowNum(); i++) {
- row = (Row) sheet.getRow(i);
- String name = row.getCell(0).getStringCellValue();
- String email = row.getCell(1).getStringCellValue();
- String sql = "INSERT INTO user
- (firstName,email,enabled,userRole,departmentId) VALUES('" + name +
- "','"+email+"')";
- pstm = (PreparedStatement) con.prepareStatement(sql);
- pstm.execute();
- System.out.println("Import rows " + i);
- }
- con.commit();
- pstm.close();
- con.close();
- input.close();
- System.out.println("Success import excel to mysql table");
- } catch (IOException e) {
- }
- }
- }
- Name email
- test test@gmail.com
- john ----
- mike mike@gmail.com
Add Comment
Please, Sign In to add comment