Guest User

Untitled

a guest
Mar 22nd, 2018
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. public class TestApp {
  2. public static void main(String[] args) throws Exception {
  3. try {
  4. Class forName = Class.forName("com.mysql.jdbc.Driver");
  5. Connection con = null;
  6. con =
  7. DriverManager.getConnection("jdbc:mysql://192.168.1.13:3307/db1",
  8. "sachin-LH", "sachin@123");
  9. con.setAutoCommit(false);
  10. PreparedStatement pstm = null;
  11. FileInputStream input = new
  12. FileInputStream("C:\Users\lh\Downloads\Student Details(2009-
  13. 13).xls");
  14. POIFSFileSystem fs = new POIFSFileSystem(input);
  15. Workbook workbook;
  16. workbook = WorkbookFactory.create(fs);
  17. Sheet sheet = workbook.getSheetAt(0);
  18. Row row;
  19. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  20. row = (Row) sheet.getRow(i);
  21. String name = row.getCell(0).getStringCellValue();
  22. String email = row.getCell(1).getStringCellValue();
  23. String sql = "INSERT INTO user
  24. (firstName,email,enabled,userRole,departmentId) VALUES('" + name +
  25. "','"+email+"')";
  26. pstm = (PreparedStatement) con.prepareStatement(sql);
  27. pstm.execute();
  28. System.out.println("Import rows " + i);
  29. }
  30. con.commit();
  31. pstm.close();
  32. con.close();
  33. input.close();
  34. System.out.println("Success import excel to mysql table");
  35. } catch (IOException e) {
  36. }
  37. }
  38. }
  39.  
  40. Name email
  41. test test@gmail.com
  42. john ----
  43. mike mike@gmail.com
Add Comment
Please, Sign In to add comment