Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 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.             java.sql.PreparedStatement stmt = con.prepareStatement();
  18.             stmt.setString(1, name);
  19.             ResultSet r = stmt.executeQuery("SELECT id FROM transit where name=?");
  20.             if (r.next()) {
  21.                 System.out.println("Такой товар уже существует");
  22.             } else {
  23.                 System.out.println("Добавлен новый товар");
  24.                 String sq = "INSERT INTO transit"
  25.                         + "(name, amount, price) VALUES"
  26.                         + "(?,?,?)";
  27.                 java.sql.PreparedStatement prepareStatement = con.prepareStatement(sq);
  28.                 prepareStatement.setString(1, name);
  29.                 prepareStatement.setInt(2, amount);
  30.                 prepareStatement.setDouble(3, price);
  31.                 prepareStatement.executeUpdate();
  32.             }
  33.  
  34.         }
  35. //        con.commit();
  36. //        con.close();
  37. //        file.close();
  38. //        System.out.println("Success import excel to mysql table");
  39.  
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement