[debutant][jdbc]driver introuvable
bonjour,
j'ai mis ds le classpath:
- le chemin permettant d'acceder au .jar contenant le driver ds le classpath.
- le chemin permettant d'arriver à la classe Driver donc ...com.mysql.jdbc.Driver.class. c'est bien ça ?
j'utilise une base de données mysql.
j'ecris mon code sous eclipse 3.0.2
mais j'ai un pb. je ne sais pas si c un pb de code ou si c'est un pb de classpath.
qd je lance le prog, ça me met
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Driver introuvable :
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Test.main(Test.java:30) |
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
|
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
...
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost"; //je ne sais pas si c bon...
String login = "";
String password = "";
Connection connection = null;
try{
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url,login,password);
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from table");
while(rs.next()){
String s = rs.getString("nom");
System.out.println(s);
}
...
}
catch(ClassNotFoundException cnfe){
System.out.println("Driver introuvable : ");
cnfe.printStackTrace();
}
catch(SQLException sqle){
System.out.println("Erreur SQL : "+sqle.getErrorCode()); }
catch(Exception e){
System.out.println("Autre erreur : ");
e.printStackTrace();
}
finally {
if(connection!=null){
try{
connection.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
} |
merci si vous pouvez m'aider