erizo_s

as

May 23rd, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1.  public static void updatePrice() throws SQLException, ClassNotFoundException, IOException {
  2.  
  3.         Class.forName("com.mysql.jdbc.Driver");
  4.         Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/testdb", "erizo", "4123");
  5.         con.setAutoCommit(false);
  6.         PreparedStatement pstm = null;
  7.         FileInputStream file = new FileInputStream("D:\\price_0002.xls");
  8.         POIFSFileSystem fs = new POIFSFileSystem(file);
  9.         HSSFWorkbook wb = new HSSFWorkbook(fs);
  10.         HSSFSheet sheet = wb.getSheetAt(0);
  11.         Row row;
  12.         for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  13.             row = sheet.getRow(i);
  14.             String name = row.getCell(0).getStringCellValue();
  15.             int amount = (int) row.getCell(1).getNumericCellValue();
  16.             double price = row.getCell(2).getNumericCellValue();
  17.             String query = "SELECT id FROM transit where name=?";
  18.             java.sql.PreparedStatement stmt = con.prepareStatement(query);
  19.             stmt.setString(1, name);
  20.             ResultSet r = stmt.executeQuery();
  21.             if (r.next()) {
  22.                 String sqq = "UPDATE transit SET price=? WHERE id =?";
  23.                 java.sql.PreparedStatement preparedStatement = con.prepareStatement(sqq);
  24.                 preparedStatement.setInt(1, i);
  25.                 preparedStatement.setDouble(2, price);
  26.                 preparedStatement.executeUpdate();
  27.             } else {
  28.                 System.out.println("Добавлен новый товар");
  29.                 String sq = "INSERT INTO transit"
  30.                         + "(name, amount, price) VALUES"
  31.                         + "(?,?,?)";
  32.                 java.sql.PreparedStatement prepareStatement = con.prepareStatement(sq);
  33.                 prepareStatement.setString(1, name);
  34.                 prepareStatement.setInt(2, amount);
  35.                 prepareStatement.setDouble(3, price);
  36.                 prepareStatement.executeUpdate();
  37.             }
  38.  
  39.         }
  40.         con.commit();
  41.         con.close();
  42.         file.close();
  43.     }
  44. }
Add Comment
Please, Sign In to add comment