jdbc:sqlite 未检索数据
我正在使用 jdbc:sqlite 访问 sqlite 数据库并通过简单的 select 语句获取一些记录。数据库中有行,但 java 函数没有返回任何内容。
private static String ConnectionString = "jdbc:sqlite:D:\\My Documents\\NetBeansProjects\\IntelligentBusStop\\BusSystem.db";
private static ResultSet GetData(String command)
{
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try
{
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection(ConnectionString);
statement = connection.createStatement();
resultSet = statement.executeQuery(command);
return resultSet;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
try
{
resultSet.close();
statement.close();
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static ArrayList<StopBS> GetStopBS()
{
ResultSet results = GetData("Select StopNumber, Tag, Latitude, Longitude FROM StopTable ");
ArrayList<StopBS> stops = new ArrayList<StopBS>();
try
{
while (results.next())
{
StopBS newStop = new StopBS();
newStop.StopNumber = results.getInt("StopNumber");
newStop.Tag = results.getString("Tag");
newStop.Lat = results.getFloat("Latitude");
newStop.Long = results.getFloat("Longitude");
stops.add(newStop);
}
return stops;
}
catch (Exception e)
{
e.printStackTrace();
return null ;
}
}
直接在数据库上使用sql语句就可以了。我正在使用 Razor sql 查看数据库,如果这有什么区别的话。
I am using a jdbc:sqlite to access a sqlite db and get some records with a simple select statement. There are rows in the database but the java function returns nothing.
private static String ConnectionString = "jdbc:sqlite:D:\\My Documents\\NetBeansProjects\\IntelligentBusStop\\BusSystem.db";
private static ResultSet GetData(String command)
{
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try
{
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection(ConnectionString);
statement = connection.createStatement();
resultSet = statement.executeQuery(command);
return resultSet;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
try
{
resultSet.close();
statement.close();
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static ArrayList<StopBS> GetStopBS()
{
ResultSet results = GetData("Select StopNumber, Tag, Latitude, Longitude FROM StopTable ");
ArrayList<StopBS> stops = new ArrayList<StopBS>();
try
{
while (results.next())
{
StopBS newStop = new StopBS();
newStop.StopNumber = results.getInt("StopNumber");
newStop.Tag = results.getString("Tag");
newStop.Lat = results.getFloat("Latitude");
newStop.Long = results.getFloat("Longitude");
stops.add(newStop);
}
return stops;
}
catch (Exception e)
{
e.printStackTrace();
return null ;
}
}
Using the sql statement directly on the database works. An i am using Razor sql to view the db if that makes any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
GetData
中关闭了ResultSet
,因此当GetStopBS
尝试将其加载到数据结构中时,光标不可用。我会重写这段代码以正确完成它。你很接近,但不在那儿。
了解 Sun 编码标准。您遵循的是 .NET 标准,而不是 Java。
当您关闭语句、结果集和连接时,您的心是在正确的位置,但我不同意您放置代码的位置。我建议永远不要将 java.sql 包引用传递到创建它们的方法范围之外。以相同的方法创建、使用和关闭它们。
您还应该在 finally 块中关闭它们,将关闭包装在单独的 try/catch 块中。
You close your
ResultSet
inGetData
, so by the timeGetStopBS
tries to load it into the data structure the cursor isn't available.I'd rewrite this code to do it properly. You're close, but not there.
Learn the Sun coding standards. You're following .NET standards, not Java.
Your heart is in the right place when you close your statement, result set, and connection, but I disagree with where you put the code. I recommend never passing java.sql package references out of the method scope in which they were created. Create them, use them, and close them in the same method.
You should also close them in a finally block, wrapping the close in individual try/catch blocks.