【问题标题】:Simple select using SQLite java driver 3.21.0 does not return data使用SQLite java驱动3.21.0的简单选择不返回数据
【发布时间】:2018-05-10 14:07:42
【问题描述】:

我在 MacOS 上使用 SQLite 和 java 发出一个非常简单的选择时遇到问题。

这是我的代码:

public class TestJDBC1 
{

public static Connection connect_DB() throws ClassNotFoundException {
    Class.forName("org.sqlite.JDBC");
    Connection conn = null;
    try {
        // db parameters
        String url = "jdbc:sqlite:/Users/Shared/DB_Farmaci/oracle-sample.db";
        // create a connection to the database
        conn = java.sql.DriverManager.getConnection(url);

        System.out.println("Connection a SQLite stabilita.");

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return conn;
}

public static void main( String[] args ) throws ClassNotFoundException, InterruptedException, SQLException
{
    Connection conn = connect_DB();
    try 
    {
      conn.setAutoCommit(false);
    } catch (SQLException ex) {
        Logger.getLogger(TestJDBC1.class.getName()).log(Level.SEVERE, null, ex);
    }

    try 
    {
        Statement S = null ;
        ResultSet rs = null ;
        S = conn.createStatement();

        String queryS = "select deptno, dname, loc from dept ;" ;
        rs = S.executeQuery(queryS);

        S.close();
        conn.close();

        int x = 0;
        while (rs.next()) 
        {
          x++;
          System.out.println(rs.getInt("deptno") +  "\t" + 
                             rs.getString("dname") + "\t" +
                             rs.getDouble("loc"));
        }
        if (x > 0) 
        {
            System.out.println("# rows returned => " + x);
        } else 
        {
            System.out.println("no rows returned");
        }
    } catch (SQLException se) {
        System.out.println(se);
        System.out.println("errore DB");
    }
}    

如您所见,它确实很简单,但它不返回任何数据。

我的环境如下:

  1. NetBeans 8.2
  2. sqlite-jdbc-3.21.0.jar(当前版本)

使用类似的代码,我可以创建表并将行插入数据库,因此环境的设置应该没问题(属性=>库=>编译和运行)。

我也在 E​​clipse 上进行了尝试,但结果相同。我还尝试了我发现的所有向后的 JDBC 驱动程序版本,同样的情况。我正在使用 SQLite 3.10.1 的 DB Browser,我可以读取我要查找的数据。

我的 Mac 上安装了 SQLite,我可以使用命令行命令读取数据。

这看起来像是驱动程序故障,除非我遗漏了一些非常重要的东西。

【问题讨论】:

    标签: java sqlite jdbc


    【解决方案1】:

    关闭连接后您将无法访问结果集。参考Java - Can't use ResultSet after connection close

    在你使用完结果集之后调用你的 close()。

        String queryS = "select deptno, dname, loc from dept ;" ;
        rs = S.executeQuery(queryS);
    
        //remove your close from here.
    
        int x = 0;
        while (rs.next()) 
        {
          x++;
          System.out.println(rs.getInt("deptno") +  "\t" + 
                             rs.getString("dname") + "\t" +
                             rs.getDouble("loc"));
        }
        if (x > 0) 
        {
            System.out.println("# rows returned => " + x);
        } else 
        {
            System.out.println("no rows returned");
        }
    
    
        //move your close here
        S.close();
        conn.close();
    

    【讨论】:

      【解决方案2】:

      在处理结果集之前关闭连接和语句。这是不可能的(当您使用 rs.next() 时应该会导致 SQLException,但您没有发布)。

      您需要将代码更改为:

      try (Connection conn = connect_DB()) {
          // other actions with connection
      
          String queryS = "select deptno, dname, loc from dept ;" ;
          try (Statement statement = conn.createStatement();
               ResultSet rs = statement.executeQuery(queryS)) {
      
              int x = 0;
              while (rs.next())  {
                  x++;
                  // etc
              }
      
          } catch (SQLException se) {
              se.printStackTrace()
              System.out.println("errore DB");
          }
      }
      

      我在这里使用了try-with-resources,因为它会简化你的代码并防止这样的错误,因为像结果集这样的资源将被限制在其父对象的范围内。在 try-with-resources 结束时,Java 会自动调用 close() 方法。

      我建议您阅读有关 JDBC 的教程并查看其 Javadoc 以获取有关如何使用 JDBC api 的更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-17
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-27
        • 1970-01-01
        • 2016-05-30
        相关资源
        最近更新 更多