bonjour ,
J'ai besoin d'un peu d'aide sur les Threads .
J'ai écris une classe permettant de faire mes recherches dans ma base SQL et je voudrais que la méthode 'executerLaRecherche()' soit executée dans un thread de manière a rendre la main avant que la requete soit terminée (car elle peut prendre beaucoup de temps : 3 à 4 minutes).
J'ai donc voulu créer un Thread , le probleme c'est que dans la methode run de mon Thread , les différents attributs de ma classe ne sont pas accessible .
Quelqu'un pourrait m'aider et me dire comment je pourrais faire ?
Merci d'avance de votre aide .
Pour info , sans le thread , ce code-ci fonctionne tres bien :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package net.dev; import java.sql.*; import net.dev.connecteurSQL;; public class rechercheSQL { private String laRequeteSQL; private Connection laConnexion; public ResultSet leResultat; public rechercheSQL () throws SQLException { this.laConnexion = null; this.laRequeteSQL = null; this.leResultat = null; } public void executerLaRecherche () throws SQLException { Thread monThread = new Thread () { public void run () { if (this.laConnexion != null && this.laRequeteSQL != null) { Statement instruction = this.laConnexion.createStatement(); this.leResultat = instruction.executeQuery(this.laRequeteSQL); } } }; monThread.start(); } public void setLaRequeteSQL (String req) throws SQLException { this.laRequeteSQL = req; } public void setlaConnexion (connecteurSQL monConnecteur) throws SQLException { this.laConnexion = monConnecteur.getConnexion(); this.leResultat = null; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package net.dev; import java.sql.*; import net.dev.connecteurSQL;; public class rechercheSQL { private String laRequeteSQL; private Connection laConnexion; public ResultSet leResultat; public rechercheSQL () throws SQLException { this.laConnexion = null; this.laRequeteSQL = null; this.leResultat = null; } public void executerLaRecherche () throws SQLException { if (this.laConnexion != null && this.laRequeteSQL != null) { Statement instruction = this.laConnexion.createStatement(); this.leResultat = instruction.executeQuery(this.laRequeteSQL); } } public void setLaRequeteSQL (String req) throws SQLException { this.laRequeteSQL = req; } public void setlaConnexion (connecteurSQL monConnecteur) throws SQLException { this.laConnexion = monConnecteur.getConnexion(); this.leResultat = null; } }
Partager