Guest User

Untitled

a guest
Jan 9th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. public void handle(ActionEvent event){
  2. FileChooser fileChooser = new FileChooser();
  3.  
  4. //Set extension filter
  5. FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
  6. FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
  7. fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
  8.  
  9. //Show open file dialog
  10. File file = fileChooser.showOpenDialog(null);
  11.  
  12. try {
  13. BufferedImage bufferedImage = ImageIO.read(file);
  14. WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);
  15. myImageView.setImage(image);
  16. } catch (IOException ex) {
  17. Logger.getLogger(CreateProductUI.class.getName()).log(Level.SEVERE, null, ex);
  18. }
  19. }
  20.  
  21. Image image = panel.getMyImageView().getImage();
  22.  
  23. public Product(String name,String desc,double price, int quantity,String datestr,Image image){
  24. this.name = name;
  25. this.desc = desc;
  26. this.price = price;
  27. this.quantity = quantity;
  28. this.datestr = datestr;
  29. this.image = image;
  30. }
  31.  
  32. public boolean create(){
  33. boolean success = false;
  34. DBController db = new DBController();
  35. String dbQuery;
  36. db.getConnection();
  37.  
  38. dbQuery = "INSERT INTO sm_product(productName,productDescription,productPrice,productQuantity,dateOfCreation,productStatus,productImage) VALUES ('" + name + "', '" + desc + "', " + price + ", " + quantity + ",'" + datestr + "', 'Available', '" + image + "')";
  39.  
  40. if (db.updateRequest(dbQuery) == 1){
  41. success = true;
  42. }
  43. db.terminate();
  44. return success;
  45. }
  46.  
  47. public void getConnection(){
  48. String url = "";
  49. try {
  50. //url = "jdbc:mysql://172.20.133.227/test";
  51. url = "jdbc:mysql://localhost/amkcc";
  52. con = DriverManager.getConnection(url, "root", "root");
  53. System.out.println("Successfully connected to " + url+ ".");
  54. }
  55. catch (java.sql.SQLException e) {
  56. System.out.println("Connection failed ->"+ url);
  57. System.out.println(e);
  58. }
  59. }
  60.  
  61. private void binsertActionPerformed(java.awt.event.ActionEvent evt) {
  62.  
  63. if(cheakInputs() && imagePath != null)
  64. {
  65. try
  66. {
  67. String query = "insert into tblproduct tblproduct(p_id,p_name,p_price,p_date,p_image) values(?,?,?,?,?)";
  68. Connection con =getConnection();
  69. PreparedStatement ps = con.prepareStatement(query);
  70.  
  71. ps.setString(1,tid.getText());
  72. ps.setString(2,tname.getText());
  73. ps.setString(3,tprice.getText());
  74. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
  75. String addDate = dateFormat.format(cdate.getDate());
  76. ps.setString(4,addDate);
  77. File file = new File(imagePath);
  78. InputStream img = new FileInputStream(file);
  79. ps.setBlob(5, img);
  80. ps.executeUpdate();
  81. JOptionPane.showMessageDialog(null,"congrats");
  82.  
  83. }
  84. catch(Exception e)
  85. {
  86. JOptionPane.showMessageDialog(null,"sory"+e.getMessage());
  87. }
  88. }
  89. else
  90. {
  91. JOptionPane.showMessageDialog(null, "onr or more text Fields empty");
  92. }
  93.  
  94. File image = new File("C:/image.jpg");
  95. psmnt = connection.prepareStatement
  96. ("insert into save_image(name, city, image, Phone) "+ "values(?,?,?,?)");
  97. psmnt.setString(1,"MyName");
  98. psmnt.setString(2,"MyCity");
  99. psmnt.setString(4,"123456");
  100. FileInputStream fis = new FileInputStream(image);
  101. psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
  102. int s = psmnt.executeUpdate();
  103.  
  104. public boolean create(){
  105. boolean success = false;
  106. try{
  107. DBController db = new DBController();
  108. String dbQuery;
  109. db.getConnection();
  110.  
  111. dbQuery = "INSERT INTO sm_product(productName,productDescription,productPrice,productQuantity,dateOfCreation,productStatus,productImage) VALUES (?,?,?,?, ?, 'Available', ?)";
  112.  
  113. PreparedStatement psmnt = db.getConnection().prepareStatement(dbQuery);
  114. psmnt.setString(1,name);
  115. psmnt.setString(2, desc);
  116. psmnt.setDouble(3, price);
  117. psmnt.setInt(4, quantity);
  118. psmnt.setString(5, dateStr);
  119.  
  120. File imageFile = new File("test.png");
  121. RenderedImage renderedImage = SwingFXUtils.fromFXImage(image, null);
  122. ImageIO.write(renderedImage, "png", imageFile); //Change extension appropriately
  123. FileInputStream fis = new FileInputStream(imageFile);
  124. psmnt.setBinaryStream(3, (InputStream)fis, (int)(imageFile.length()));
  125. int s = psmnt.executeUpdate();
  126.  
  127. //check the value of s and initialize success appropriately
  128.  
  129. return success;
  130. }catch(Exception e) { e.printStackTrace(); }
  131. return false;
  132.  
  133. }
  134.  
  135. public Connection getConnection(){
  136. if(con != null) return con;
  137. String url = "";
  138. try {
  139. //url = "jdbc:mysql://172.20.133.227/test";
  140. url = "jdbc:mysql://localhost/amkcc";
  141. con = DriverManager.getConnection(url, "root", "root");
  142. System.out.println("Successfully connected to " + url+ ".");
  143. return con;
  144. }
  145. catch (java.sql.SQLException e) {
  146. System.out.println("Connection failed ->"+ url);
  147. System.out.println(e);
  148. }
  149. return null;
  150. }
Add Comment
Please, Sign In to add comment