Connection JAVA avec MySQL

Fermé
cool-abdoul Messages postés 96 Date d'inscription dimanche 14 juin 2009 Statut Membre Dernière intervention 30 novembre 2010 - 27 oct. 2009 à 12:40
cool-abdoul Messages postés 96 Date d'inscription dimanche 14 juin 2009 Statut Membre Dernière intervention 30 novembre 2010 - 27 oct. 2009 à 15:23
Bonjour les amis,

Je n'arrive pas à récupérer mes données dans MySQL avec mon application JAVA. Je ne suis pas un pro de JAVA, soyez indulgents les amis !! Le problème, c'est qu'il n'y a pas d'erreurs dans la console de Netbeans car il me met : " BUILD SUCCESSFUL (total time: 1 second) ". Mais mes données ne s'affichent pas avec mon " System.out.println(result.getString(1)+" --- "+ result.getString(2)); " Je ne sais pas si mon problème vient de là !! Aidez moi les amis !!!


Voici mon code :



package com;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
//import com.sun.jndi.ldap.Connection;
//import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
*
* @author acoul
*/

public class NewClass {

public static void main (String [] args) throws ClassNotFoundException, SQLException{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:formation_stagiare";
Connection con = (Connection) DriverManager.getConnection(url);

Statement sta = (Statement) con.createStatement();
String requete = "SELECT description,titre_cours FROM cours ORDER BY titre_cours";
ResultSet result = sta.executeQuery(requete);

while (result.next()) {
System.out.println(result.getString(1)+" --- "+ result.getString(2));
}
} catch(SQLException s){
s.getMessage();
}

}
}
A voir également:

4 réponses

cool-abdoul Messages postés 96 Date d'inscription dimanche 14 juin 2009 Statut Membre Dernière intervention 30 novembre 2010 9
27 oct. 2009 à 14:13
au secours les amis !!
1
scriptiz Messages postés 1424 Date d'inscription dimanche 21 décembre 2008 Statut Membre Dernière intervention 14 septembre 2023 425
27 oct. 2009 à 12:41
Regarde comment faire à cet endroit :

https://www.heimetli.ch/jdbc/JDBCQuery.html
0
cool-abdoul Messages postés 96 Date d'inscription dimanche 14 juin 2009 Statut Membre Dernière intervention 30 novembre 2010 9
27 oct. 2009 à 13:57
Merci de me repondre, j'ai fait le test du lien mais j'ai ce message :

class sun.jdbc.odbc.JdbcOdbcDriver
Exception in thread "main" java.lang.NullPointerException
at com.NewClass.main(NewClass.java:46)

Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

A mon avis , c'est là que ça coince : con = (Connection) DriverManager.getConnection(url);
voici mon code modifié :


package com;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
//import com.sun.jndi.ldap.Connection;
//import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
*
* @author acoul
*/
public class NewClass {

public static void main (String [] args) throws ClassNotFoundException, SQLException{
Connection con = null ;
Statement sta = null ;
ResultSet result = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:formation_stagiare";

con = (Connection) DriverManager.getConnection(url);

sta = (Statement) con.createStatement();
String requete = "SELECT description,titre_cours FROM cours ORDER BY titre_cours";
result = sta.executeQuery(requete);

while (result.next()) {
System.out.println(result.getString(1)+" --- "+ result.getString(2));
}
} catch(SQLException s){
s.getMessage();
}

finally {
result.close() ;
sta.close() ;
con.close() ;

}

}
}
0
cool-abdoul Messages postés 96 Date d'inscription dimanche 14 juin 2009 Statut Membre Dernière intervention 30 novembre 2010 9
27 oct. 2009 à 15:23
çà marche !!!! voici la solution !!!!



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
*
* @author acoul
*/

public class NewClass {


public static void main (String [] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException{
Connection con = null ;
Statement sta = null ;
ResultSet result = null;
try {

Class.forName("org.gjt.mm.mysql.Driver");

String url = "jdbc:mysql://localhost/tabase";



con = (Connection) DriverManager.getConnection(url,"user","motdepasse");



sta = (Statement) con.createStatement();

String requete = "SELECT description,titre_cours FROM cours ORDER BY titre_cours";

result = sta.executeQuery(requete);

while (result.next()) {

System.out.println(result.getString(1)+" --- "+ result.getString(2));
}

result.close() ;
sta.close() ;
con.close() ;

} catch(SQLException s){
s.getMessage();
}


}
}
0