Juan
Juan

Reputation: 2099

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?

The stack trace:

java.sql.SQLException: no such table: controller
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NestedDB.prepare(NestedDB.java:115)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.executeQuery(Stmt.java:89)
at app.ConexionBD.consultar(ConexionBD.java:37)
at app.controllers.InicioController.buildTableView(InicioController.java:145)

The table does exist in database

Upvotes: 0

Views: 846

Answers (1)

Constantin
Constantin

Reputation: 1506

Try to fully qualify your table name like OWNER.TABLE_NAME

Make sure the account you connect with has SELECT privilege granted to it from the table owner

Those are my two obvious suggestions

Brute force is to examine all tables like so:

    DatabaseMetaData md = connection.getMetaData();
    ResultSet rs = md.getTables(null, null, "%", null);
    while (rs.next()) {
      System.out.println(rs.getString(3));
    }

Upvotes: 1

Related Questions