JDBC返回一个空的ResultSet(rs.isBeforeFirst()== true),尽管表不为空 [英] JDBC returns an empty ResultSet (rs.isBeforeFirst() == true) although the table isn't empty
问题描述
我尝试为我的Web服务完成数据库访问方法。服务和DB访问方法对DB中的所有其他表工作正常,但这一个特定的方法不会。当我查询DB时, ResultSet
总是返回空(表示 isBeforeFirst()== true
)。
I am trying to complete a DB access method for my Web Service. The service and the DB access methods work fine for all other tables in the DB, but this one particular method does not. When I query the DB, the ResultSet
always returns empty (meaning isBeforeFirst() == true
).
经过多次尝试,我将我的查询切换到一个简单的 SELECT * FROM VIDEOS
,看看这些数据是否有一些区别我输入的数据和我在查询中使用的数据,但即使这个简单的查询选择表中的所有项目也没有返回任何结果。
After many tries I cut my query to a simple SELECT * FROM VIDEOS
to see if the problem is some difference between the data I entered and the data I use in my query, but even this simple query to select all items in the table didn't return any result.
这是方法I使用从DB中拉取信息:
This is the method I use to pull info from the DB:
public static Object[] getVideo(String phonenum, String timeStamp)
{
Connection c = null;
Statement stmt = null;
Object[] result = null;
try
{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:lineappDB.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String query = String.format("SELECT * FROM VIDEOS");
ResultSet rs = stmt.executeQuery(query);
// If no data was found
if (rs.isBeforeFirst())
{
rs.close();
stmt.close();
c.close();
return null;
} else
{
result = new Object[6];
while (rs.next())
{
result[0] = rs.getInt(1);
result[1] = rs.getString(2);
result[2] = rs.getString(3);
result[3] = rs.getString(4);
result[4] = rs.getString(5);
result[5] = rs.getInt(6);
}
}
rs.close();
stmt.close();
c.close();
} catch (Exception e)
{
try
{
if (!c.isClosed())
{
c.commit();
c.close();
}
} catch (Exception ex)
{
System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
return null;
}
System.err.println(e.getClass().getName() + ": " + e.getMessage());
return null;
}
System.out.println(String.format("Succesfully pulled from DB - %s %s", result[1], result[2]));
return result;
}
任何帮助将非常感激。
CLARIFICATION EDIT:
该方法是Web服务的一部分,它从DB中拉出某个视频的路径,发送给客户端。视频由客户端上传,然后存储在文件系统中,其路径存储在数据库本身中。
CLARIFICATION The method is part of a web service that pulls a path of a certain video from the DB, to be sent to a client. The videos are uploaded by clients and are then stored in the filesystem, and their paths are stored in the DB itself.
一旦我看到数据库工作,我将替换 SELECT * FROM VIDEOS
with SELECT * FROM VIDEOS WHERE PHONENUM ='%s'AND DATETIME ='%s',phonenum,timeStamp
,以便查询提取我需要的确切项。
Once I see the DB works, I will replace SELECT * FROM VIDEOS
with SELECT * FROM VIDEOS WHERE PHONENUM = '%s' AND DATETIME = '%s'", phonenum, timeStamp
so that the query pulls the exact item I need.
推荐答案
isBeforeFirst()
如果下一次调用 next()
会将光标置于<$ c $的第一行,则返回 true
c> ResultSet 。换句话说,任何具有数据的成功查询,一旦执行,将产生 ResultSet
with isBeforeFirst ()
返回 true
。
isBeforeFirst()
returns true
if the next call to next()
will put the cursor on the first row of the ResultSet
. In other words, any successful query that has data, once executed, will produce a ResultSet
with isBeforeFirst()
returning true
.
只需从代码中删除该块, rs.next()
循环处理潜在的空 ResultSet
s:
Just remove that block from your code, and have the rs.next()
loop deal with potentially empty ResultSet
s:
try
{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:lineappDB.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String query = String.format("SELECT * FROM VIDEOS");
result = new Object[6];
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
result[0] = rs.getInt(1);
result[1] = rs.getString(2);
result[2] = rs.getString(3);
result[3] = rs.getString(4);
result[4] = rs.getString(5);
result[5] = rs.getInt(6);
}
}
/* catch and finally snipped for clarity of the answer */
这篇关于JDBC返回一个空的ResultSet(rs.isBeforeFirst()== true),尽管表不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!