Erreur "non-static variable this cannot be referenced from a static context"
Bonjour,
Je suis en train de développer une application Java qui prend en entré 2 bases de données Microsoft Access que l'utilisateur choisit avec un bouton Parcourir. Cette application doit faire des traitements et ensuite on reçoit comme output un fichier Excel.
Mon problème est que je n'arrive pas à tester la fonction que j'ai développée et dans le main une erreur s'affiche
Citation:
non-static variable this cannot be referenced from a static context
Voilà mon code source :
graphique est la JFrame que j'ai créé et jTextField2 c'est le champs où je récupère le chemin de la base choisie.
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 55 56 57 58 59 60 61 62 63
| package nokia;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JFrame;
/**
* @author Sami
*/
public class ConnectionDbDyn {
// Chemin de la base
private String path;
// Connection vers la base
private Connection connection;
/* Constructeur */
public ConnectionDbDyn (String path) {
this.path = path;
}
public void connect() {
try {
// Chargement du driver ODBC
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Connexion à la base
String connectionString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" + path+";";
connection = DriverManager.getConnection(connectionString, "","");
Statement st = connection.createStatement();
String sql = "Select * from A_ADCE";
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
System.out.println( md.getColumnCount() );
while(rs.next()) {
System.out.println("\n"+rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
} catch (ClassNotFoundException e) {
System.out.println("Problème avec le driver ODBC");
//return false;
} catch (SQLException e) {
System.out.println("Impossible de se connecter à la base");
// return false;
}
//return true;
}
public static void main(String[] args) {
ConnectionDbDyn c = new ConnectionDbDyn(graphique.jTextField2.getText()) ;
}
} |