Salut tout le monde, vous allez bien?
J'ai un probleme et c'est un truc de fou !
Pour faire des acces a ma base de donnée je passe normalement par une classe qui s'appelle DBConnection.
Ca fait plus d'une semaine que j'avais pas touché au projet (j'ai surement du faire quelque chose, mais je vois pas) et la ca ne marche plus !
Du coup j'ai créé quelques fonctions de tests pour cerner le problème mais je trouve pas.
Ce n'est pas tres long, c'est du code assez simple avec 2 logs d'erreurs.
Voici ce que j'ai fais :
Ici c'est la page d'accueil de test qui permet de tester les acces à la base de donnée.
Test.jsp :
Les 2 boutons du dessus permettent d'acceder au servlet test que j'ai fais à la vite pour essayer de comprendre le pb.
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 <HTML> <HEAD> <TITLE>DataBase Test!</TITLE> </HEAD> DataBase Test Button <FORM ACTION="http://localhost:8080/Training/TEST2"> <INPUT TYPE="SUBMIT" VALUE = "test"> </FORM> DataBase Test Button2 <FORM ACTION="http://localhost/Training/TEST3"> <INPUT TYPE="SUBMIT" VALUE = "test"> </FORM> </BODY> </HTML>
Les voici :
TEST2.java :
Ce code marche parfaitement, il me renvoi bien les nom des utilisateur de la base de donnée avec le nombre de ligne du getRow.
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 Training; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.sql.DriverManager; public class TEST2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); Statement stmt; ResultSet result; String url = "jdbc:mysql://localhost:3306/HeroDB"; String user = "Login"; String password = "Password"; try{ Class.forName("com.mysql.jdbc.Driver"); out.println("<br> DRIVERS JDBC : OK!"); Connection connection = DriverManager.getConnection(url,user,password); out.println("<br> Databse Connexion : OK!"); String req ="Select * FROM User_Table"; stmt = connection.createStatement(); result = stmt.executeQuery(req); // Partie rajouté pour voir si le Resultat retourne bien un nombre de ligne superieur a 0 int numRows = 0; result.last(); numRows = result.getRow(); result.first(); out.println(numRows); result.beforeFirst(); while (result.next()) { out.println(result.getString("Login")); } } catch (ClassNotFoundException e) { out.println("Driver non chargé !"); } catch(SQLException ex) { out.println("<br> MESSAGE D'ERREUR <br>"); while (ex != null) { out.println("<br>Message: " + ex.getMessage ()); out.println("<br>SQLState: " + ex.getSQLState ()); out.println("<br>ErrorCode: " + ex.getErrorCode ()); ex = ex.getNextException(); out.println(""); } } } }
Donc pas de souci, mais c'est la suite qui est plus interessante :
Passons à la suite, la ou je galère !
TEST3.java :
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 package Training; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.sql.DriverManager; public class TEST3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); // New Instance : DBConnection DBConnection db = new DBConnection(); String req ="Select * FROM User_Table"; try{ db.Connect(); out.println("<br> DRIVERS JDBC : OK!"); out.println("<br> Connexion à la BDD : OK!"); ResultSet Result = db.QueryDataBase(req); // Partie rajouté pour voir si le Resultat retourne bien un nombre de ligne superieur a 0 int col = db.TotalRows(Result); if (col == 0) { out.println("<br>Query to DB is not ok!<br>"); } else{ out.println(col); } } catch (ClassNotFoundException e) { out.println("Driver non chargé !"); } catch (Exception x) { out.println(x); } } }
DBConnection.java
Alors là, je ne comprend pas pourquoi ca ne marche pas... Le resultat retourné pour le nombre de ligne est 0
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 package Training; import java.sql.*; import java.sql.DriverManager; public class DBConnection{ Connection connection; Statement stmt; ResultSet result; public DBConnection(){ } public void Connect() throws Exception, SQLException{ String url = "jdbc:mysql://localhost:3306/HeroDB"; String user = "Login"; String password = "Password"; try { // Load JDBC Drivers Class.forName("com.mysql.jdbc.Driver"); // make the connection with the database Connection connection = DriverManager.getConnection(url,user,password); } catch(SQLException sqle){ System.out.println(sqle.getMessage()); } catch(Exception e){ System.out.println("The Connection Failed !"+ e.getMessage()); } } public ResultSet QueryDataBase(String SQLFunc)throws SQLException, Exception{ try{ stmt = connection.createStatement(); result = stmt.executeQuery(SQLFunc); } catch(SQLException sqle){ System.out.println("Problem with getting Result1!"+ sqle.getMessage()); System.out.println("Problem with getting Result2!"+ sqle.getSQLState()); System.out.println("Problem with getting Result3!"+ sqle.getErrorCode()); } catch(Exception e){ System.out.println("Problem with getting Result4!"+ e.getMessage()); } return result; } public int TotalRows (ResultSet rs) throws SQLException, Exception { int numRows = 0; try{ rs.last(); numRows = rs.getRow(); rs.first(); } catch(SQLException sqle) { System.out.println("Problem with getting Row1"+ sqle.getMessage()); System.out.println("Problem with getting Row2"+ sqle.getSQLState()); System.out.println("Problem with getting Row3"+ sqle.getErrorCode()); } catch(Exception e){ System.out.println("Problem with getting getting Row4!"+ e.getMessage()); } return numRows; } }![]()
Pourquoi le premier code fonctionnerait alors que le 2eme non ? C'est pas normal, y'a quasisement aucune différence.
Voici les logs d'erreur de Tomcat et MySQL :
MySQL :
TOMCAT :Version: '4.0.20a-nt' socket: '' port: 3306
050315 19:15:52 Aborted connection 4 to db: 'herodb' user: 'Login' host: `localhost' (Got an error reading communication packets)
050315 19:16:42 Aborted connection 1 to db: 'herodb' user: 'Login' host: `localhost' (Got an error reading communication packets)
Pour info : j'utilise Tomcat 5.0.28, Mysql 4.0.20 et Java 1.5Mar 15, 2005 7:10:46 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading this Context has started
Mar 15, 2005 7:10:46 PM org.apache.catalina.logger.LoggerBase stop
INFO: unregistering logger Catalina:type=Logger,path=/monk2,host=localhost
NotifyUtil::java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at org.netbeans.modules.web.monitor.server.NotifyUtil$RecordSender.run(NotifyUtil.java:237)
NotifyUtil::java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at org.netbeans.modules.web.monitor.server.NotifyUtil$RecordSender.run(NotifyUtil.java:237)
NotifyUtil::java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at org.netbeans.modules.web.monitor.server.NotifyUtil$RecordSender.run(NotifyUtil.java:237)
Problem with getting Result4!null
Problem with getting getting Row4!null
NotifyUtil::java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at org.netbeans.modules.web.monitor.server.NotifyUtil$RecordSender.run(NotifyUtil.java:237)
Voila voila, si vous pouviez m'aider à trouver le pb, au moins me donner des pistes car jsuis dessus depuis un ptit moment, et les boites d'aspirine se vident à la vitesse V.
Merci d'avance.
++
ShinJava
Partager