| 12
 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
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 
 | package javaphpconnector;
 
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.JOptionPane;
 
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
 
 
/**
 *
 * @author Nicolas
 */
public class C_Connector {
 
    public C_Connector() {
        this.listNomColonne = new ArrayList<>();
 
    }
 
    public void mysqlQuery(String req) {
        try {
            url = new URL(connector);
            Map<String,Object> params = new LinkedHashMap<>();
            params.put("sql", req);
            params.put("private_key", private_key);
 
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String,Object> param : params.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
 
            conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(postDataBytes);
 
            saxBuilder = new SAXBuilder();
 
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            document = saxBuilder.build(reader);
 
            rootNode = document.getRootElement();
            recordList = rootNode.getChildren("record");
            getInfoRecords();
 
        } catch (JDOMException | IOException e) {  
            JOptionPane.showMessageDialog(null,e.toString()+"\n","Erreur",JOptionPane.INFORMATION_MESSAGE); 
        }
 
    }
 
	// Fonction pour recuperer les noms et le nombre de champs
	// nbCol -> nombre de colonne
	// listNomColonne -> list des noms de colonne
      public void getInfoRecords() {   
        Iterator<Element> i;
        Iterator<Element> i2;
        nbCol = 0;
        i = recordList.iterator();
        if (i.hasNext()) {
            Element courant = i.next();
            List<Element> listCol = courant.getChildren();
            nbCol = listCol.size();
            i2 = listCol.iterator();
            while(i2.hasNext()) {
                Element courant2 = i2.next();
                listNomColonne.add(courant2.getName());
            }
         }
    }
	// Fonction pour recuperer le id en cas insert
    public String getNewId() {
        String rt = rootNode.getChild("newid").getText();
        return rt;
    }
	// Fonction pour recuperer le nombre de rtow
    public String getCnt() { // directement en dessous du root
        String rt = rootNode.getChild("cnt").getText();
        return rt;
    }
	// Fonction pour recuperer l'erreur si il y en a une
    public String getErr() { // directement en dessous du root
        String rt = rootNode.getChild("error").getText();
        return rt;
    }
	// Fonction pour recuperer le nombre de colonne
    public int getNbCol() {
        return nbCol;
    }
	// Fonction pour recuperer les nom des colonnes
    public ArrayList<String> getNomsColonnes() {
        return listNomColonne;
    }
	// Fonction pour recuperer toutes les valeurs d'une colonne
    public String getCol(String nom) {
        String rt="";
        Iterator<Element> i;
        i = recordList.iterator();
        while(i.hasNext()) {
            Element courant = i.next();
            rt += courant.getChild(nom).getText();
         }
        return rt;
    }
 
private final String private_key = "maclef";
private final String USER_AGENT = "Mozilla/5.0";    
private final String connector = "http://localhost/monfichier.php";
private SAXBuilder saxBuilder; 
private Document document;
private Element rootNode;
private List<Element> recordList;
private int nbCol;
private ArrayList<String> listNomColonne;
 
private URL url;
private HttpURLConnection conn;
private OutputStreamWriter writer;
private BufferedReader reader;
} | 
Partager