Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 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.                 System.out.println("Такой товар уже существует");
  23.             } else {
  24.                 System.out.println("Добавлен новый товар");
  25.                 String sq = "INSERT INTO transit"
  26.                         + "(name, amount, price) VALUES"
  27.                         + "(?,?,?)";
  28.                 java.sql.PreparedStatement prepareStatement = con.prepareStatement(sq);
  29.                 prepareStatement.setString(1, name);
  30.                 prepareStatement.setInt(2, amount);
  31.                 prepareStatement.setDouble(3, price);
  32.                 prepareStatement.executeUpdate();
  33.             }
  34.  
  35.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement