Mysql java jdbc driver

Résolu
Loftus Messages postés 4 Date d'inscription   Statut Membre Dernière intervention   -  
Loftus Messages postés 4 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour j'arrive pas à établir la connexion java et jdbc !

J'ai une BD Mysql 5.6

pilote jdbc 5.1.25 sous forme de jar que j'ai décompressé et ajouté à ma librairie

Je travaille sous Netbeans

Voici mon code :

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Elevage","root","pass");
Statement st= con.createStatement();
String query="Select * from Animal;";

ResultSet rs=st.executeQuery(query);
while (rs.next()) {
String c1=rs.getNString("id");
String c2=rs.getNString("espece");
String c3=rs.getNString("sexe");
String c4=rs.getNString("date_naissance");
String c5=rs.getNString("nom");
String c6=rs.getNString("commentaires");

model.addRow(new Object[]{c1,c2,c3,c4,c5,c6});

}

rs.close();
st.close();
con.close();

}
catch(Exception e ){

JOptionPane.showMessageDialog(this,"Error in connectivity");
}
A voir également:

3 réponses

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
J'imagine que tu obtiens un message "Error in connectivity", mais il serait bien d'en avoir le détail, en affichant le contenu de l'exception "e".

catch (Exception e)
{
    StringBuilder sb = new StringBuilder(e.toString());
	
    for (StackTraceElement trace : e.getStackTrace())
        sb.append("\n    at ").append(trace.toString());
	
    JOptionPane.showMessageDialog(null, sb, "Error in connectivity", JOptionPane.ERROR_MESSAGE, null);
}
0
Loftus Messages postés 4 Date d'inscription   Statut Membre Dernière intervention  
 
euuh merci KK j'ai déja résolu le probleme c'est loin de ça en fait ..Il est au niveau de l'exécution de la requête ;) en remplaçant le "getNstring " par "getString" tt simplement :)

Merci quand même
0
Loftus Messages postés 4 Date d'inscription   Statut Membre Dernière intervention  
 
Maintenant j'ai un autre problème : à chaque fois que je vx ajouter ds lignes ou modifier ma DB ça marche pas sachant que j'utilise bien les instructions correspondantes aux requêtes SQL
un exemple d'insertion d'une ligne dans la table Animal :

Statement state = con.createStatement();
state.executeUpdate("INSERT INTO Animal VALUES(9,'vache','F','2010-04-05 13:43:00','Rox','sans oreilles');");
Aidez moi svp et mrci d'avance
0