Plusieurs problèmes avec base de données
Bonjour, je dois développer un projet web pour un organisme. Etant débutant, cela est d'autant plus difficile que je dois me former en même temps. J'ai donc une classe chargée de récupérer la liste des stagiaires présents actuellement dans l'organisme à partir d'une base mysql:
Code:
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
| package app;
import java.sql.*;
public class Stagiaire {
public Stagiaire(int pid) {
try {
this.database = new Database("localhost","root","","base");
Connection connexion = this.database.connect();
this.query = connexion.createStatement();
this.query.executeQuery("SELECT nom, prenom, datenaissance FROM stagiaire WHERE id = "+pid);
ResultSet datas = this.query.getResultSet();
if(datas == null) throw new SQLException("Pas de stagiaire correspondant");
else {
this.id = pid;
this.nom = datas.getString(1);
this.prenom = datas.getString(2);
this.date = datas.getString(3);
}
}
catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
}
catch(SQLException e) {
System.out.println("Erreur: le stagiaire id "+pid+" n'existe pas");
}
}
public static Stagiaire [] getList() {
Stagiaire StagiaireList[] = {null};
int i = 0;
try {
Database db = new Database("localhost","root","","base");
Connection conn = db.connect();
Statement statement = conn.createStatement();
statement.executeQuery("SELECT id, nom, prenom, datenaissance FROM stagiaire ORDER BY nom");
ResultSet datas = statement.getResultSet();
while(datas.next()) {
StagiaireList[i++] = new Stagiaire(datas.getInt(1));
}
}
catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
}
catch(SQLException e) {
System.out.println(e.getMessage());
}
return StagiaireList;
}
public int getID() {return this.id;}
public String getNom() {return this.nom;}
public String getPrenom() {return this.prenom;}
public String getDate() {return this.date;}
private int id;
private String nom;
private String prenom;
private String date;
private Database database;
private Statement query;
} |
et une jsp chargée de communiquer avec cette classe:
Code:
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
| <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="app.*"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Administration</title>
</head>
<body>
<%
String action = request.getParameter("action");
if(action != null && action.equals("view")) {
int id = Integer.parseInt(request.getParameter("id"));
Stagiaire stagiaire = new Stagiaire(id);
%>
Nom: <%= stagiaire.getNom() %>
<br />
Prénom: <%= stagiaire.getPrenom() %>
<%
}
else {
Stagiaire [] liste = Stagiaire.getList();
if(liste == null) out.println("Aucun stagiaire dans la base de données");
else {
%>
<table class="listeStagiaire" align="center" cellpadding="5" cellspacing="1">
<tr>
<th class="listeStagiaire" align="center">
NOM
</th>
<th class="listeStagiaire" align="center">
PRENOM
</th>
<th class="listeStagiaire" align="center">
DATE
</th>
</tr>
<%
for(Stagiaire s : liste) {
if(s != null) {
%>
<tr>
<td class="listeStagiaire">
<%= s.getNom() %>
</td>
<td class="listeStagiaire">
<%= s.getPrenom() %>
</td>
<td class="listeStagiaire">
<%= s.getDate() %>
</td>
</tr>
<%
}
}
out.println("</table>");
}
}
%>
</body>
</html> |
A l'exécution de cette dernière, le serveur JBoss me renvoit une erreur concernant un débordement de tableau dans la classe ci-dessus à la ligne suivante:
Code:
StagiaireList[i++] = new Stagiaire(datas.getInt(1));
Je ne comprend pas comment il peut y avoir ce débordement. Je sais également qu'il peut y avoir pas mal d'autres erreurs dans mon code. Pouvez vous m'aider svp? Merci.