Bonjour,
J'ai écrit le code suivant qui me permet de me connecter à une base de donnée puis d'afficher les les éléments de la requete sql.
Le soucis que j'ai c'est que j'ai mis une taille arbitraire dans mon tableau
String datas [][] = new String [150][ncols];
car je ne sais pas comment faire pour récupérer le nombre de lignes.
Est-ce que quelqu'un pourrait m'indiquer une méthode pour pouvoir connaitre le nombre de lignes qui ont été obtenues.
Merci
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
|
import java.sql.*;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class Table{
public static void main(String arg[]){
//Parametre de connexion a la base de données
String url="jdbc:odbc:MAGIC";
String login = "root";
String password = "root";
Connection connection = null;
try{
Driver monDriver = new JdbcOdbcDriver();
DriverManager.registerDriver(monDriver);
connection=DriverManager.getConnection(url,login,password);
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("SELECT Name AS Nom, COUNT([Seq.Open
By]) AS Ouvert FROM MAGIC._SMDBA_.Incident, MAGIC._SMDBA_._PERSONNEL_
WHERE MAGIC._SMDBA_.Incident.[Seq.Open By] =
MAGIC._SMDBA_._PERSONNEL_.SEQUENCE GROUP BY Name");
creerTable(rs);
rs.close();
stmt.close();
connection.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
connection.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
public static void creerTable(ResultSet rs) throws SQLException{
int i;
ResultSetMetaData rsmd = rs.getMetaData();
int ncols = rsmd.getColumnCount();
String [] noms = new String[ncols];
for(i=1; i<=ncols; i++){
noms[i-1] = rsmd.getColumnLabel(i);
}
String datas [][] = new String [150][ncols];
boolean suivant = rs.next();
int k=0;
while(suivant){
for(i=1; i<=ncols; i++){
datas[k][i-1] = rs.getString(i);
}
suivant = rs.next();
k++;
}
for(int j=0; j<noms.length; j++){
System.out.println(noms[j]);
}
System.out.println(datas.length);
System.out.println(datas[0].length);
for(int l=0;l<datas.length; l++)
for(int m=0;m<datas[0].length; m++){
System.out.println(datas[l][m] + " ");
}
}
} |
Partager