Bonjour,
Je me permet de vous écrire ce message pour m'aider ou bien de me mettre sur la voie. Je développe une application qui m'affiche juste mes données de ma table.
Sur éclipse ça fonctionne bien, mais sur inteljj il me renvoie une erreur et je comprends pas pourquoi. Merci de votre aide que vous pourrez m'apporter.
J'utilise Dao et le design pattern.
Voici mon code:
Ma classe AbstractDAOfactory
Ma classe Demande
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 package com.company; public abstract class AbstractDAOfactory { private static AbstractDAOfactory factory; public static AbstractDAOfactory getFactory() { return factory; } public static void setFactory(AbstractDAOfactory factory) { AbstractDAOfactory.factory = factory; } public abstract DemandeDao createD(); }
Mon interface DemandeDao
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 package com.company; import java.util.List; import java.util.Objects; public class Demande { private int id; private String user; private String password; public int getId(int i) { return id; } public String getUser() { return user; } public String getPassword() { return password; } public void setId(int id) { this.id = id; } public void setUser(String user) { this.user = user; } public void setPassword(String password) { this.password = password; } public Demande(int id, String user, String password) { this.id = id; this.user = user; this.password = password; } public Demande() { } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Demande demande = (Demande) o; return id == demande.id && Objects.equals(user, demande.user) && Objects.equals(password, demande.password); } @Override public int hashCode() { return Objects.hash(id, user, password); } @Override public String toString() { return "Demande{" + "id=" + id + ", user='" + user + '\'' + ", password='" + password + '\'' + '}'; } public List<Demande> lister() { AbstractDAOfactory factory = AbstractDAOfactory.getFactory(); DemandeDao demandedao = factory.createD(); return demandedao.lister(); } }
Ma classe DemandeDaoImpl
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 package com.company; import java.util.List; public interface DemandeDao { List<Demande> lister(); }
Ma classe MySqlDaoFactory
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 package com.company; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class DemandeDaoImpl implements DemandeDao { private static DemandeDaoImpl instance; public static DemandeDaoImpl getInstance(){ if(instance == null) instance = new DemandeDaoImpl(); return instance; } @Override public List<Demande> lister() { List<Demande> demand = new ArrayList<Demande>(); Connection connexion = null; Statement statement = null; ResultSet resultat = null; try { connexion = MySqlDaoFactory.getInstance().getConnection(); statement = connexion.createStatement(); resultat = statement.executeQuery("SELECT * FROM demande"); while (resultat.next()) { int id = resultat.getInt("id"); String user = resultat.getString("user"); String password = resultat.getString("password"); Demande demande = new Demande(); demande.setId(id); demande.setUser(user); demande.setPassword(password); demand.add(demande); } } catch (SQLException e) { e.printStackTrace(); } finally{ MySqlDaoFactory.closeStatement(statement); MySqlDaoFactory.closeConnection(connexion); MySqlDaoFactory.closeResultSet(resultat); } return demand; } }
Ma classe main
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 package com.company; import java.sql.*; public class MySqlDaoFactory extends AbstractDAOfactory { private static MySqlDaoFactory instance; public static Connection getConnection() { String url = "jdbc:mysql://localhost:3306/garage"; String user = "root"; String password = ""; Connection c = null; try { Class.forName("com.mysql.jdbc.Driver"); c = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException | SQLException cnfe) { System.out.println("test:" + cnfe.getMessage()); } return c; } public static MySqlDaoFactory getInstance() { if (instance == null) { instance = new MySqlDaoFactory(); } return instance; } public static void closeResultSet(ResultSet rs) { try { if (rs != null) { rs.close(); } } catch (SQLException e) { } } public static void closeStatement(Statement st) { try { if (st != null) { st.close(); } } catch (SQLException e) { } } public static void closeConnection(Connection c) { try { if (c != null) { c.close(); } } catch (SQLException e) { } } //cette classe extends de ma classe abstraite et qui fait appel a la methode @Override public DemandeDaoImpl createD() { return DemandeDaoImpl.getInstance(); } }
Ma table
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 package com.company; public class Main { public static void main(String[] args) { AbstractDAOfactory.setFactory(MySqlDaoFactory.getInstance()); Demande d = new Demande(); System.out.println(d.lister()); } }
Merci
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 -- phpMyAdmin SQL Dump -- version 4.7.9 -- https://www.phpmyadmin.net/ -- -- Hôte : 127.0.0.1 -- Généré le : ven. 01 mai 2020 à 18:07 -- Version du serveur : 10.1.31-MariaDB -- Version de PHP : 7.2.3 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Base de données : `garage` -- -- -------------------------------------------------------- -- -- Structure de la table `demande` -- CREATE TABLE `demande` ( `id` int(11) NOT NULL, `user` varchar(250) NOT NULL, `password` varchar(250) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Déchargement des données de la table `demande` -- INSERT INTO `demande` (`id`, `user`, `password`) VALUES (1, 'user', 'user'), (2, 'test', 'test'), (3, 'test', 'test'), (4, 'tettt', 'tttt'); -- -- Index pour les tables déchargées -- -- -- Index pour la table `demande` -- ALTER TABLE `demande` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT pour les tables déchargées -- -- -- AUTO_INCREMENT pour la table `demande` -- ALTER TABLE `demande` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Partager