简体   繁体   English

Java 数据库为空时崩溃

[英]Java crash when database is empty

I am using the SQLite JDBC driver to access a database, for some reason the application terminates if there are no rows in the database with little explanation as to why it happened.我正在使用 SQLite JDBC 驱动程序来访问数据库,由于某种原因,如果数据库中没有行,应用程序将终止,几乎没有解释它发生的原因。

If the database is not empty it works perfectly fine;如果数据库不是空的,它工作得很好;

This is where the termination occurs:这是终止发生的地方:

public static CustomFillTable Select(String table, String[] column, Object operand) throws SQLException
    {
        Connection connection = null;
        PreparedStatement ps = null;

        try
        {  
            StringBuilder query = new StringBuilder();

            query.append("select ");

            for(int i = 0; i < column.length; i++)
            {
                query.append(column[i]);

                if(i < column.length -1)
                {
                    query.append(",");
                }
            }

            query.append(" from ");
            query.append(table);

            //Verify usage of where clause
            if(operand != null)
            {
                query.append(" where ");
                query.append(operand);
            }

            //Termination occurs here
            connection = DriverManager.getConnection(_connectionString);
            connection.setAutoCommit(false);
            ps = connection.prepareStatement(query.toString());
            ResultSet rs = ps.executeQuery();
            connection.commit();
            CustomFillTable model = new CustomFillTable(rs);
            while(rs.next())
            {
                System.out.println("id = " + rs.getString("id"));
                System.out.println("name = " + rs.getString("name"));
            }

            return model;
        }

The application closes at the DriverManager.getConnection line, something I don't find to be related to whether the database is populated or not.应用程序在 DriverManager.getConnection 行关闭,我发现这与是否填充数据库无关。

Does anybody know how to fix this problem?有谁知道如何解决这个问题? I've posted the log dump information here .我在这里发布了日志转储信息。

Edit:编辑:

Connection String -连接字符串 -

"jdbc:sqlite:D:\Documents\Uni\Semester 2\Languages, Platforms and Tools\Assignment\Java\SQLiteAssignment\mydatabasefile.db" “jdbc:sqlite:D:\Documents\Uni\Semester 2\Languages, Platforms and Tools\Assignment\Java\SQLiteAssignment\mydatabasefile.db”

After looking at the vendors website it seems it is something that has been addressed in a later build.在查看供应商网站后,它似乎已在以后的构建中解决。 I ended up changing driver anyway but I thought this might be of some use to people.无论如何,我最终还是更换了驱动程序,但我认为这可能对人们有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM