1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package html;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
*
* @author Haithem
*/
public class ProduitPanel extends JPanel {
public JLabel img,title,prix;
Connection connect ;
ResultSet rs;
static String url = "jdbc:mysql://localhost:3306/memoire";
static String user ="root";
static String password="";
public String getPrix(){
return prix.getText();
}
public String getTitle(){
String titre = null;
Document doc = Jsoup.parse(title.getText(),"UTF-8");
Elements titles = doc.select("h2");
titre = titles.text();
return titre;
}
public String getUrl(){
String url = null;
Document doc = Jsoup.parse(title.getText(),"UTF-8");
Elements lien = doc.select("a");
url = lien.attr("href").toString();
return url;
}
public ProduitPanel(int i,int j){
super();
this.setBounds(i, j , 800,250);
img = new JLabel("IMAGE");
title = new JLabel("TITLE");
title.setBackground(Color.BLUE);
prix = new JLabel("PRIX");
img.setSize(50, 50);
//img.setBounds(10, 20, 50,200);
//title.setBounds(60, 20, 50 , 80);
//prix.setBounds(100, 120, 50, 50);
title.setSize(50,50);
prix.setSize(50,50);
// img.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
// title.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
// prix.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
this.add(img);
this.add(title);
this.add(prix);
this.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
System.out.println(((ProduitPanel)evt.getSource()).getPrix().substring(0,((ProduitPanel)evt.getSource()).getPrix().length()-1 ) + "\t" + ((ProduitPanel)evt.getSource()).getUrl() + "\t" + ((ProduitPanel)evt.getSource()).getTitle() );
try{
connect = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO `memoire`.`produit` (`prix`, `title`, `lien`) VALUES (? , ? , ?)";
PreparedStatement pstmt = connect.prepareStatement(sql);
pstmt.setDouble(1, Double.parseDouble(((ProduitPanel)evt.getSource()).getPrix().substring(0,((ProduitPanel)evt.getSource()).getPrix().length()-1 )));
pstmt.setString(2, ((ProduitPanel)evt.getSource()).getTitle());
pstmt.setString(3, ((ProduitPanel)evt.getSource()).getUrl());
pstmt.executeQuery(sql);
}catch(SQLException ex){
ex.getMessage();
}
}
});
this.setLayout(new GridLayout());
}
@Override
public String toString(){
return this.title.getText();
}
} |
Partager