OSSOUBB
Messages postés29Date d'inscriptionsamedi 7 mars 2009StatutMembreDernière intervention28 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 = "";
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.