JavaFX 8 Tutorial 52 - Import Excel to Database





The code written in this tutorial is :



private Button importXLToDB;



importXLToDB = new Button("Import XL TO DB");

        importXLToDB.setFont(Font.font("Sanserif", 15));

        importXLToDB.setOnAction(e -> {

            try {

                String query = "Insert into UserDatabase(ID, FirstName, LastName, Email) values (?,?,?,?)";

                pst = conn.prepareStatement(query);

               

                FileInputStream fileIn = new FileInputStream(new File("UserInfo.xlsx"));

               

                XSSFWorkbook wb = new XSSFWorkbook(fileIn);

                XSSFSheet sheet = wb.getSheetAt(0);

                Row row;

                for(int i=1; i<=sheet.getLastRowNum(); i++){

                    row = sheet.getRow(i);

                    pst.setInt(1, (int) row.getCell(0).getNumericCellValue());

                    pst.setString(2, row.getCell(1).getStringCellValue());

                    pst.setString(3, row.getCell(2).getStringCellValue());

                    pst.setString(4, row.getCell(3).getStringCellValue());

                    pst.execute();

                }

                Alert alert = new Alert(AlertType.INFORMATION);

                alert.setTitle("Information Dialog");

                alert.setHeaderText(null);

                alert.setContentText("User Details Imported From Excel Sheet To Database.");

                alert.showAndWait();

               

                wb.close();

                fileIn.close();

                pst.close();

                rs.close();

            } catch (SQLException | FileNotFoundException ex) {

                Logger.getLogger(Tutorial14.class.getName()).log(Level.SEVERE, null, ex);

            } catch (IOException ex) {

                Logger.getLogger(Tutorial14.class.getName()).log(Level.SEVERE, null, ex);

            }

            refreshTable();

        });

       

        HBox hbox = new HBox(5);

        hbox.getChildren().addAll(load, delete,update, comboBox, exportToXL, importXLToDB);

       

Comments

  1. thank u for your video , pz i want to jar library
    thank u again

    ReplyDelete

Post a Comment

Popular posts from this blog

JavaFX 8 Tutorial 62 - Jasper Report

JavaFX 8 Tutorial 64 Spring Boot CRUD with FXML