Bonjour tout le monde;

Je développe une application Java avec MySQL.

Je désire utiliser une classe de connexion générique, c'est à dire ne pas avoir à ecrire à chaque connexion toutes les lignes de codes de la connexion mais juste instancier la classe de connexion que j'ai appelée ConnectDB.

Dans un autre fichier.java ou j'instancie cette classe pour faire un aller retour vers ma base de données, je fais qu'instancier un objet de la connexion comme convenu.

Mais une erreur surgit : NullPointerException

Merci pour votre patience et pour votre aide.

Voici ma classe ConnetDB :

============>

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
45
46
47
48
49
50
51
52
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package maquette_statique;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author lhabib
 */
public class ConnectDB {
    ResultSet rs = null;
    Connection con = null;
    Statement stmt = null;
 
    public void connect(String queryString) {
        try {
// ---- configure this for your site
            String username = "root";
            String password = "admin";
// The URL that will connect to TECFA’s MySQL server
// Syntax: jdbc:TYPE:machine:port/DB_NAME
            String url = "jdbc:mysql://localhost:3306/decompte";
// A canned query string
            queryString = null;
// ---- configure END
// INSTALL/load the Driver (Vendor specific Code)
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (java.lang.ClassNotFoundException e) {
                System.err.print("ClassNotFoundException: ");
                System.err.println(e.getMessage());
            }
 
// Connection to the database at URL with usename and password
            con = DriverManager.getConnection(url, username, password);
            System.out.println("Ok, connection to the DB worked.");
            System.out.println("Let’s see can retrieve something with: " + queryString);
// Create a Statement Object
            stmt = con.createStatement();
            // Send the query and bind to the result set
	ResultSet rs = (ResultSet) stmt.executeQuery(queryString);
 
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
***********************

Remarquez que j'ai laissé en paramètre la requête sql qui dépend du fichier fichier qui instancie l'objet de connexion.

************************

A présent voici mon fichier .java qui instancie la connexion ConnectDB :

==================>

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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package maquette_statique;
 
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.Label;
import com.sun.webui.jsf.component.TextField;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import java.sql.*;
 
 
public class Authentification extends AbstractPageBean {
 
//ùùùùùùùùùùùùùùùùùùùùùùùùùùùùùù
 
    String email = new String();
    String password = new String();
 
    String loginBD = new String();
    String passwordBD = new String();
 
    ConnectDB connexion = new ConnectDB();
 
//ùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùù    
 
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
 
    private void _init() throws Exception {
    }
    private TextField textField1 = new TextField();
 
    public TextField getTextField1() {
        return textField1;
    }
 
    public void setTextField1(TextField tf) {
        this.textField1 = tf;
    }
    private TextField textField2 = new TextField();
 
    public TextField getTextField2() {
        return textField2;
    }
 
    public void setTextField2(TextField tf) {
        this.textField2 = tf;
    }
    private Label label3 = new Label();
 
    public Label getLabel3() {
        return label3;
    }
 
    public void setLabel3(Label l) {
        this.label3 = l;
    }
 
    // </editor-fold>
 
    public Authentification() {
    }
 
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
 
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
 
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }
 
    @Override
    public void preprocess() {
    }
    @Override
    public void prerender() {
    }
    @Override
    public void destroy() {
    }
 
  //ùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùù  
 
    public String button1_action() {
 
        try {
            String email = null;
            String password = null;
            String loginBD = null;
            String passwordBD = null;
 
            email = (String) textField1.getText();
            password = (String) textField2.getText();
 
 
            connexion.connect("select * from intervenant");
 
/* =========> */           while (connexion.rs.next()) {
 
                loginBD = connexion.rs.getString("login");
                passwordBD = connexion.rs.getString("password");
 
                if (email.equals(loginBD) && password.equals(passwordBD)) {
                    break;
                }
            }
 
        } catch (SQLException ex) {
            Logger.getLogger(Authentification.class.getName()).log(Level.SEVERE, null, ex);
        }
 
        if (email.equals(loginBD) && password.equals(passwordBD)) {
                return "case1";
            } else {
                label3.setText("authentification invalide");
                textField1.setText("");
                textField2.setText("");
                return null;
            }
 
 
    }
//ùùùùùùùùùùùùùùùùùùùùùùùùùùùùùù
}
**************************************************

Notez que les lignes de codes dont il s'agit sont encadrées par des

//ùùùùùùùùùùùùùùùùùùùùùùùùùùùùùùù

les autres sont a négliger.

De plus le compilateur signale que l'erreur se situe au niveau de la ligne ou j'ai mis /* =========> */