how can i read and add image in dataBase using javafx in table view?

Viewed 23

The Class Prodect :

import javafx.scene.image.Image;

public class Prodect implements Comparable<Prodect> {

    private String ID;
    private String name;
    private double Purchase;
    private int numCopy;
    private double price;
    private double proft;

    private double total;
    private int quantity;
    private double totalproft;
    private Image Image;
    

    
    public Prodect(String iD, String name, double purchase, int numCopy, double price , Image Image) {
        super();
        ID = iD;
        this.name = name;
        Purchase = purchase;
        this.numCopy = numCopy;
        this.price = price;
        this.Image = Image;

    }
    
    
    
    

    public Image getImage() {
        return Image;
    }

    public void setImage(Image image) {
        Image = image;
    }

    
    public int compareTo(Prodect o) {
        return this.name.compareTo(o.name);
    }

}

The Function to read the data from dataBase i think i dont Know how to read the Image using dataBase (I am a Beginner)


private void readDataBaseProdect() throws SQLException, ClassNotFoundException {
        Connection con = db.getConnection().connectDB();
        try {
            String sql = "SELECT * FROM Prodect";
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                
                
                ProdectList.add(
                        new Prodect(rs.getString(1), rs.getString(2), rs.getDouble(3), rs.getInt(4), rs.getDouble(5) , new Image(rs.getBlob(6).getBinaryStream())));

            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

The function to add image into the tableView and the dataBase

try {
                    
                    File f = new File(path);
                    ImageView ii = new ImageView(path);
                    
                    Image img = ii.getImage();
                    
                    
                    
                    Connection con = db.getConnection().connectDB();
                    
                    String sql = "insert into Prodect (ID,Name,Purchase,NumCopy,price,Imags) values('" + tex5.getText()
                            + "','" + tex6e.getText() + "','" + tex7e.getText() + "','" + tex8e.getText() + "','"
                            + AT.getText() + "','"+img+"')";
                    
                    ProdectList.add(new Prodect(tex5.getText(), tex6e.getText(), Double.parseDouble(tex7e.getText()) ,
                            Integer.parseInt( tex8e.getText()), Double.parseDouble(AT.getText()),img));
                    
                    
                    Statement stmt = con.createStatement();
                    stmt.executeUpdate(sql);
                
                } catch (Exception m) {
                    System.out.println(m);
                    }

The table in dataBase

create table Prodect(
ID varchar(32) primary key,
Name varchar(32),
Purchase real,
NumCopy int,
price real,
proft real ,
total real,
quantity int ,
totalProft real ,
Image longblob
        
);

What is the wrong with my code ? Please Help me with my issue I tried so hard to get this but i couldn't

0 Answers
Related