manana007
10 avril 2008 à 10:43
Bonjour,
j'essaie de remplir un jCombox à partir d'une base de donnée (serveur MySQL) mais en affichant l'interface j'ai un boucle infinie je ne peut pas corriger cette erreur
SVP aider moi à résoudre ce problème (je travaille en NetBeans)
voici le code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package asterisk_teste;
/**
*
* @author
*/
import java.sql.*;
import java.io.*;
import java.util.*;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.event.ListDataListener;
public class SQLrequete
{ public static Administrateur ad =new Administrateur();
public ResultSet rs1 ;
public SQLrequete() throws SQLException
{
try {
Connection connection;
BaseDeDonnee basededonnee = new BaseDeDonnee ();
rs1=basededonnee.rs;
} catch (ClassNotFoundException ex) {
Logger.getLogger(SQLrequete.class.getName()).log(Level.SEVERE, null, ex);
}
// ad.Terminal.setModel(model);
// ad.setVisible(true);
}
public static void main(String[] args) throws SQLException
{ //SQLrequete teste =new SQLrequete();
try {
//Connection connection;
BaseDeDonnee basededonnee = new BaseDeDonnee ();
// teste.rs1=basededonnee.rs;
// teste.model=basededonnee.getListModelFromResultSet(basededonnee.rs);
} catch (ClassNotFoundException ex) {
Logger.getLogger(SQLrequete.class.getName()).log(Level.SEVERE, null, ex);
}
//teste.ad.Terminal.setModel(model);
// teste.ad.setVisible(true);
}
}
class BaseDeDonnee
{
public Connection connection = null ;
public ResultSet rs ;
public Statement stmt;
public String requete;
public Administrateur ad =new Administrateur();
public DefaultListModel model;
public BaseDeDonnee() throws ClassNotFoundException, SQLException
{ System.out.println("\n+++++++++++++++++++");
connexion();
System.out.println("\n++++++++++++++++++++++");
stmt = connection.createStatement();
requete = "SELECT * FROM terminal ORDER BY Num ASC LIMIT 0 , 30";
System.out.println("\n=======================");
rs=affichage(requete);
this.getModelFromResultSet(affichage(requete));
ad.setVisible(true);
System.out.println("\n====================");
fin_connexion();
}
// Connexion à la base de donnees
public void connexion () throws ClassNotFoundException, SQLException
{
try
{
// Chargement des drivers SQL
System.out.println("\n------------------------");
System.out.println("Connexion au driver JDBC.");
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Driver com.mysql.jdbc.Driver chargé.");
}
catch (ClassNotFoundException a)
{
System.out.println ("Driver non trouve.");
}
catch (Exception b)
{
System.out.println ("Problème sur chargement de driver JDBC.");
}
try
{
// Etablissement de la connexion avec la base
String url1 = "jdbc:mysql://127.0.0.1:3306/Asteriskteste";
connection = DriverManager.getConnection(url1,"root","bandia007");
System.out.println("Connexion a la base établie.");
System.out.println("------------------------\n");
}
catch (SQLException c)
{
System.out.println ("Connexion refuse ou base inconnu.");
}
catch (Exception d)
{
System.out.println ("Problème sur connexion.");
}
}
public ResultSet affichage(String req)
{
// ResultSet rs = null;
int i=1;
try
{
//Exécution des requêtes
//requete = "INSERT INTO `Terminal` ( `Terminal_nom` ) VALUES ( 'EGG' )";
//requete = "SELECT * FROM terminal ORDER BY Num ASC LIMIT 0 , 30";
rs = stmt.executeQuery(requete);
// Statement stmt = connection.createStatement() ;
//String queryString = "INSERT INTO terminal ( Nom ) VALUES ( 'softphone_mouna' )";
// String nom ="401";
// String nom1 ="400";
// String queryString = "select Num from terminal " ;
// String queryString1 =req;
//int update = stmt.executeUpdate(queryString);
//int update1 = stmt.executeUpdate(queryString1);
System.out.println (rs);
while (rs.next())
{
System.out.println("Nom : " +rs.getString(1));
System.out.println("Num : " +rs.getString(2));
System.out.println("State :" +rs.getString(3));
System.out.println(""+i);
i++;
}
}
catch (SQLException d)
{ d.getSQLState();
d.getErrorCode();
System.out.println ("Problème la requete.");
System.out.println ("Problème la requete." +d.getErrorCode()+d.getSQLState());
}
return rs;
}
public void getModelFromResultSet(ResultSet rs) throws SQLException {
ComboBoxModel model = new ComboBoxModel() {
public void setSelectedItem(Object anItem) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object getSelectedItem() {
throw new UnsupportedOperationException("Not supported yet.");
}
public int getSize() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object getElementAt(int index) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void addListDataListener(ListDataListener l) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void removeListDataListener(ListDataListener l) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
while (rs.next()) {
rs.getString("NomDeLaColonne");
ad.NumDial.addItem(rs.getString(1));
}
}
public void fin_connexion()
{
try
{
// Fermeture de la connexion
connection.close();
System.out.println("\n------------------------");
System.out.println ("Fermeture de connexion.");
System.out.println("------------------------\n");
}
catch (Exception d)
{
System.out.println ("Problème sur la fermeture de connexion.");
}
}
}
merci d'avance