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();
- String query = "SELECT id FROM transit where name=?";
- java.sql.PreparedStatement stmt = con.prepareStatement(query);
- stmt.setString(1, name);
- ResultSet r = stmt.executeQuery();
- if (r.next()) {
- String sqq = "UPDATE transit SET price=? WHERE id =?";
- java.sql.PreparedStatement preparedStatement = con.prepareStatement(sqq);
- preparedStatement.setInt(1, i);
- preparedStatement.setDouble(2, price);
- preparedStatement.executeUpdate();
- } 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();
- }
- }
Add Comment
Please, Sign In to add comment