Bonjour,
N'ayant pas réussi à faire une connexion jdbc avec mysql sous windows, je prévois d'utiliser une base de données hsqldb pour une démonstration (deux tables Comptes et Client pour commencer). J'ai juste un fichier jar pour le Driver Jdbc.
Je rechercherais un exemple de fichier hibernate.cfg.hbm (et pom.xml) pour démarrer.
Merci pour vos commentaires
--UPDATE:
Je vais faire plus simple en revenant au connecteur MySQL sous Windows 10. J'ai validé les points suivants:
+ installation de MySQL via l'outil MySQL Workbench. J'ai du préciser un mot de passe. Le port est bien 3306. Avec l'interface Workbench j'ai pu créer une source de donnée "eltechdb" avec une seule table: tblemployees
+ écriture d'une classe JdbcModel.java qui n'est rien d'autre qu'un wrapper qui prend les attributs "datassource" "username" "password' .. à sa construction et fait un try.. catch sur DriverManager.getconection rien d'autre
+ écriture d'une classe de Test JdbcModelTest.java pour junit. Code suivant:
+ compilation sous Maven: success, puis mvn test: failure: (voir ci-dessous)
J'ai vérifié le password, les message indiquent un accès refusé.
Je n'ai pas beaucoup d'expérience en java. Merci beaucoup
Messages renvoyés:
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 package com.javatpoint.models; import com.javatpoint.models.Emp; import com.javatpoint.models.JdbcModel; import java.sql.*; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.extensions.TestSetup; public class JdbcModelTest extends TestCase { // Ceci evite de creer l'instance dans chaque methode de tests et simplifie donc l ecriture des cas de tests. private JdbcModel jdbcmdl; public JdbcModelTest() { super(); } // protected void setUp() ? public void setUp() throws Exception { super.setUp(); jdbcmdl= new JdbcModel("localhost" , "eltechdb", "fred", "lpf6lmsql") ; } public void tearDown() throws Exception { super.tearDown(); jdbcmdl = null; } public void testEmp() { assertNotNull("L instance n'est pas creee", jdbcmdl); } public void testGetPort() { assertEquals("Le port est incorrect", 3306, jdbcmdl.getPort()); } // problem here: connect returns void public void testConnect() { // assertNotNull("La connection est echouee", jdbcmdl.connect()); try { jdbcmdl.connect(); } catch (SQLException e) { System.out.println("La connection est echouee"); e.printStackTrace(); } } public static Test suite() { // pattern decorateur Pour des besoins particuliers, il peut etre necessaire d executer du code une seule fois avant l execution des cas de tests et/ou d executer du code une fois tous les cas de tests executes. TestSetup setup = new TestSetup(new TestSuite(JdbcModelTest.class)) { protected void setUp() throws Exception { // code execute une seule fois avant l execution des cas de tests System.out.println("Appel de la methode setUp() de la classe de tests"); } protected void tearDown() throws Exception { // code execute une seule fois apres l execution des cas de tests System.out.println("Appel de la methode tearDown() de la classe de tests"); } }; return setup; } public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } }
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 Running com.javatpoint.models.JdbcModelTest Appel de la methode setUp() de la classe de tests La connection est echouee java.sql.SQLException: Access denied for user 'fred'@'localhost' (using password: YES) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230) at com.javatpoint.models.JdbcModel.connect(JdbcModel.java:36) at com.javatpoint.models.JdbcModelTest.testConnect(JdbcModelTest.java:43) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:577) at junit.framework.TestCase.runTest(TestCase.java:168) at junit.framework.TestCase.runBare(TestCase.java:134) at junit.framework.TestResult$1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:243) at junit.framework.TestSuite.run(TestSuite.java:238) at junit.extensions.TestDecorator.basicRun(TestDecorator.java:24) at junit.extensions.TestSetup$1.protect(TestSetup.java:23) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.extensions.TestSetup.run(TestSetup.java:27) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:577) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Partager