SQLException在Java SQLite中执行查询

时间:2015-05-06 10:56:23

标签: java sqlite jdbc

我的NetBeans项目中有这个类:

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;
   }
}

这个使用它的方法:

    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");
       }

连接方法似乎工作正常,但是当它执行调用“executeQuery(sql)”方法的方法时,它会抛出SQLException。

我认为我配置了jdbc驱动程序,ojdbc库和数据库很好,但是我找不到为什么该方法不起作用。任何线索?

堆栈跟踪:

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)

该表确实存在于数据库

1 个答案:

答案 0 :(得分:1)

尝试完全限定您的表名,如OWNER.TABLE_NAME

确保您连接的帐户具有从表所有者

授予的SELECT权限

这是我的两个明显的建议

蛮力是检查所有表格,如下所示:

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