JAVA JDBC

Résolu
OSSOUBB Messages postés 29 Date d'inscription samedi 7 mars 2009 Statut Membre Dernière intervention 28 avril 2011 - 4 sept. 2010 à 17:36
 Pfff - 2 févr. 2015 à 10:07
Bonjour je veux me connecter à une base de données access à partir de java, voici mon code:

import java.sql.*;

/**
* This class provides an example of the code used to access a Microsoft Access database from Java through JDBC
* @author Francois Gagnon
*
*/
public class DataBaseExample
{
/**
* The URL of the database. Here we use the ODBC reference as configured in the DataSource (ODBC) of the computer
*/
private static final String DATABASE_URL = "jdbc:odbc:MaBDPatate";

/**
* A username allowed to access the database. Usually left blank
*/
private static final String DATABASE_USERNAME = "";

/**
* The password for the username allowed to access the database. Usually left blank
*/
private static final String DATABASE_PASSWORD = "";


public static void main(String[] args)
{
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;


try
{
//Establishes a valid connection with the database
connection = DriverManager.getConnection(DATABASE_URL, DATABASE_USERNAME, DATABASE_PASSWORD);

//Creates a statement that can be used to query the database
statement = connection.createStatement();

//Queries the database
resultSet = statement.executeQuery("SELECT * FROM Students ORDER BY Age");

int counter = 1;

//While the resultset has one more row:
while(resultSet.next())
{
//get the value (String) at columne named "FamiliName" for the current row
String lastName = resultSet.getString("FamilyName");

//get the value (String) at columne named "FirstName" for the current row
String firstName = resultSet.getString("FirstName");

//get the value (int) at columne named "Age" for the current row
int age = resultSet.getInt("Age");

//Instead of just printing the content, we could transform each row into an Object of type Student and return a Vector<Student>.
System.out.println(counter + " - " + firstName + ", " + lastName + ", " + age);
counter++;
}
}
catch(SQLException sqle)
{
System.err.println("ERROR whith the database connection:");
sqle.printStackTrace();
}
finally
{
try
{
//Properly close the database (to release the lock in Windows).
resultSet.close();
statement.close();
connection.close();
}
catch(Exception e)
{
System.err.println("ERROR while closing the database:");
e.printStackTrace();
}
}
}
}


mais j'ai l'erreur suivante:

[i]ERROR whith the database connection:
java.sql.SQLException: [Microsoft][Gestionnaire de pilotes ODBC] Source de données introuvable et nom de pilote non spécifié
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at DataBaseExample.main(DataBaseExample.java:36)
ERROR while closing the database:
java.lang.NullPointerException
at DataBaseExample.main(DataBaseExample.java:73)/i

J'ai besoin d'un coup de main pour débloquer cela, merci.

3 réponses

hinanos Messages postés 24 Date d'inscription jeudi 18 octobre 2007 Statut Membre Dernière intervention 15 décembre 2010
4 sept. 2010 à 21:16
j'ai comme l'impression qu'il manque un chargement du Driver nan ? genre un Class.forName ?

Et puis s'assurer que le driver est dans le classpath

@+.
3
OSSOUBB Messages postés 29 Date d'inscription samedi 7 mars 2009 Statut Membre Dernière intervention 28 avril 2011
4 sept. 2010 à 22:52
Ok, merci j'ai pu résoudre mon pb, javais mal fait la connection à la base de donnée au niveau du driver.
0
hinanos Messages postés 24 Date d'inscription jeudi 18 octobre 2007 Statut Membre Dernière intervention 15 décembre 2010
5 sept. 2010 à 09:22
tu peux poster la solution ça pourrait en intéresser d'autres ?

@+.
0
Et non, il ne peut pas. "Il" demande de l'aide, mais quand ça peut aider les autres ...
0
Rejoignez-nous