mercredi 6 mai 2015

SQLException executing query in Java SQLite

I have this class in my NetBeans project:

public class ConexionBD {
private Connection conexion;
private Statement consulta;
private final String ruta;
private final String claseDriver;
private final String driver;

public ConexionBD(String ruta) {
    this.ruta = ruta;
    this.claseDriver = "org.sqlite.JDBC";
    this.driver = "jdbc:sqlite:";
}

public void conectar() throws SQLException, ClassNotFoundException {
    Class.forName(this.claseDriver);
    this.conexion = DriverManager.getConnection(this.driver + this.ruta);
    System.out.println("Opened database successfully");
    this.consulta = this.conexion.createStatement();
    System.out.println("Statement created successfully");
}

public ResultSet consultar(String sql) throws SQLException{
    ResultSet resultado = this.consulta.executeQuery(sql);
    System.out.println(this.consulta.getConnection());
    return resultado;
}
}

And this method which use it:

private void buildTableView(){
    ConexionBD c = new ConexionBD(System.getProperty("user.dir") + "bbdd" );
    ObservableList<ObservableList> data = FXCollections.observableArrayList();
    System.out.println(System.getProperty("user.dir") + "\\bbdd");
    try{
        c.conectar(); //WORKS FINE
    }catch(ClassNotFoundException | SQLException e){
        Constantes.showAlert(AlertType.ERROR, "Error", "Error de base de datos", "Error de conexión");
    }

    try{
        String sql = "select name from controller";

        ResultSet rs = c.consultar(sql); //THROWS SQLException

        while(rs.next()){
            ObservableList<String> row = FXCollections.observableArrayList();
            row.add(rs.getString(1));
            data.add(row);
        }
        reguladores.setItems(data);
    }catch(SQLException e){
        Constantes.showAlert(AlertType.ERROR, "Error", "Error de base de datos", "Error al obtener los datos");
    }

    try {
        c.cerrar();
    } catch (SQLException ex) {
        Constantes.showAlert(AlertType.ERROR, "Error", "Error de base de datos", "Error al cerrar la conexión");
    }

The connection method seems to work fine, but when it executes the method which calls the "executeQuery(sql)" method, it throws the SQLException.

I think I configured the jdbc driver, ojdbc library and database fine, but I can't find why the method doesn't do its job. Any clue?

Aucun commentaire:

Enregistrer un commentaire