Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void handle(ActionEvent event){
- FileChooser fileChooser = new FileChooser();
- //Set extension filter
- FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
- FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
- fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
- //Show open file dialog
- File file = fileChooser.showOpenDialog(null);
- try {
- BufferedImage bufferedImage = ImageIO.read(file);
- WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);
- myImageView.setImage(image);
- } catch (IOException ex) {
- Logger.getLogger(CreateProductUI.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- Image image = panel.getMyImageView().getImage();
- public Product(String name,String desc,double price, int quantity,String datestr,Image image){
- this.name = name;
- this.desc = desc;
- this.price = price;
- this.quantity = quantity;
- this.datestr = datestr;
- this.image = image;
- }
- public boolean create(){
- boolean success = false;
- DBController db = new DBController();
- String dbQuery;
- db.getConnection();
- dbQuery = "INSERT INTO sm_product(productName,productDescription,productPrice,productQuantity,dateOfCreation,productStatus,productImage) VALUES ('" + name + "', '" + desc + "', " + price + ", " + quantity + ",'" + datestr + "', 'Available', '" + image + "')";
- if (db.updateRequest(dbQuery) == 1){
- success = true;
- }
- db.terminate();
- return success;
- }
- public void getConnection(){
- String url = "";
- try {
- //url = "jdbc:mysql://172.20.133.227/test";
- url = "jdbc:mysql://localhost/amkcc";
- con = DriverManager.getConnection(url, "root", "root");
- System.out.println("Successfully connected to " + url+ ".");
- }
- catch (java.sql.SQLException e) {
- System.out.println("Connection failed ->"+ url);
- System.out.println(e);
- }
- }
- private void binsertActionPerformed(java.awt.event.ActionEvent evt) {
- if(cheakInputs() && imagePath != null)
- {
- try
- {
- String query = "insert into tblproduct tblproduct(p_id,p_name,p_price,p_date,p_image) values(?,?,?,?,?)";
- Connection con =getConnection();
- PreparedStatement ps = con.prepareStatement(query);
- ps.setString(1,tid.getText());
- ps.setString(2,tname.getText());
- ps.setString(3,tprice.getText());
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
- String addDate = dateFormat.format(cdate.getDate());
- ps.setString(4,addDate);
- File file = new File(imagePath);
- InputStream img = new FileInputStream(file);
- ps.setBlob(5, img);
- ps.executeUpdate();
- JOptionPane.showMessageDialog(null,"congrats");
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(null,"sory"+e.getMessage());
- }
- }
- else
- {
- JOptionPane.showMessageDialog(null, "onr or more text Fields empty");
- }
- File image = new File("C:/image.jpg");
- psmnt = connection.prepareStatement
- ("insert into save_image(name, city, image, Phone) "+ "values(?,?,?,?)");
- psmnt.setString(1,"MyName");
- psmnt.setString(2,"MyCity");
- psmnt.setString(4,"123456");
- FileInputStream fis = new FileInputStream(image);
- psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
- int s = psmnt.executeUpdate();
- public boolean create(){
- boolean success = false;
- try{
- DBController db = new DBController();
- String dbQuery;
- db.getConnection();
- dbQuery = "INSERT INTO sm_product(productName,productDescription,productPrice,productQuantity,dateOfCreation,productStatus,productImage) VALUES (?,?,?,?, ?, 'Available', ?)";
- PreparedStatement psmnt = db.getConnection().prepareStatement(dbQuery);
- psmnt.setString(1,name);
- psmnt.setString(2, desc);
- psmnt.setDouble(3, price);
- psmnt.setInt(4, quantity);
- psmnt.setString(5, dateStr);
- File imageFile = new File("test.png");
- RenderedImage renderedImage = SwingFXUtils.fromFXImage(image, null);
- ImageIO.write(renderedImage, "png", imageFile); //Change extension appropriately
- FileInputStream fis = new FileInputStream(imageFile);
- psmnt.setBinaryStream(3, (InputStream)fis, (int)(imageFile.length()));
- int s = psmnt.executeUpdate();
- //check the value of s and initialize success appropriately
- return success;
- }catch(Exception e) { e.printStackTrace(); }
- return false;
- }
- public Connection getConnection(){
- if(con != null) return con;
- String url = "";
- try {
- //url = "jdbc:mysql://172.20.133.227/test";
- url = "jdbc:mysql://localhost/amkcc";
- con = DriverManager.getConnection(url, "root", "root");
- System.out.println("Successfully connected to " + url+ ".");
- return con;
- }
- catch (java.sql.SQLException e) {
- System.out.println("Connection failed ->"+ url);
- System.out.println(e);
- }
- return null;
- }
Add Comment
Please, Sign In to add comment