Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void updatePrice() throws SQLException, ClassNotFoundException, IOException {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/testdb", "erizo", "4123");
- con.setAutoCommit(false);
- PreparedStatement pstm = null;
- FileInputStream file = new FileInputStream("D:\\price_0002.xls");
- POIFSFileSystem fs = new POIFSFileSystem(file);
- HSSFWorkbook wb = new HSSFWorkbook(fs);
- HSSFSheet sheet = wb.getSheetAt(0);
- Row row;
- for (int i = 1; i <= sheet.getLastRowNum(); i++) {
- row = sheet.getRow(i);
- String name = row.getCell(0).getStringCellValue();
- int amount = (int) row.getCell(1).getNumericCellValue();
- double price = row.getCell(2).getNumericCellValue();
- java.sql.PreparedStatement stmt = con.prepareStatement();
- stmt.setString(1, name);
- ResultSet r = stmt.executeQuery("SELECT id FROM transit where name=?");
- if (r.next()) {
- System.out.println("Такой товар уже существует");
- } else {
- System.out.println("Добавлен новый товар");
- String sq = "INSERT INTO transit"
- + "(name, amount, price) VALUES"
- + "(?,?,?)";
- java.sql.PreparedStatement prepareStatement = con.prepareStatement(sq);
- prepareStatement.setString(1, name);
- prepareStatement.setInt(2, amount);
- prepareStatement.setDouble(3, price);
- prepareStatement.executeUpdate();
- }
- }
- // con.commit();
- // con.close();
- // file.close();
- // System.out.println("Success import excel to mysql table");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement