Previously, I connected to SQL Server 2012 Views and Tables properly, but few days ago, I tried to select from a Stored Procedure, with the help of my good friends here, but no chance. So i forgot about Stored Procedure and tried a View. I want to select data from a View. Here is my code:
statement = connectionHelper1.getMyConnection().createStatement();
ResultSet resultSet = statement.executeQuery("select Latitude from View_4 where IMEI='xxxxx'");
while (resultSet.next())
{
i++;
}
Toast.makeText(getApplicationContext(), " : " + i, Toast.LENGTH_SHORT).show();
This code is in button.setOnClickListener
method. When I press the button it should print the I variable, but it does nothing.
But when I print this Query in SQL Server 2012 query mode, it returns the values and Resultset works. (I mean this select query)
I should say that this View(View_4) by itself select from a table that has about 28,000,000 row!!
But when I select from another View named View_2 and select some other fields related to this View(View_2), it returns the I and ResultSet.
Notice that View_2 is another View and contains different fields from View_4.
Like this:
statement = connectionHelper1.getMyConnection().createStatement();
ResultSet resultSet = statement.executeQuery("select Latitude from View_2 where name='xxxxxxx'");
while (resultSet.next()) {
i++;
}
Toast.makeText(getApplicationContext(), " : " + i, Toast.LENGTH_SHORT).show();
The code below returns values and result from query,then it prints the I. But in first code that I have used View_4 it does not work. I notice again that the View_4 returns value from a table that has 28,000,000 rows. So I decided to select from that table directly, but still can not select from that table. Can the number of rows be the cause. If yes what can I do?
How to execute a query on existing connection
.
String sql = "select Latitude from View_4 where IMEI='xxxxx'";
Statement std = null;
ResultSet rs = null;
logger.info("executeQuery - sql=" + sql);
try {
std = connection.createStatement();
rs = std.executeQuery(sql);
logger.info("executeQuery - The query is executed");
while (rs.next()) {
String lat = rs.getString("Latitude");
logger.info("executeQuery - Latitude=" + lat);
}
logger.info("executeQuery - Query End");
} catch (SQLException e) {
logger.error("executeQuery", e); //log the full error
} finally {
if (rs != null) {
try {rs.close();} catch (SQLException sqle) {}
}
if (std != null) {
try {std.close();} catch (SQLException sqle) {}
}
}
In this code example you will see in the log after "executeQuery - sql = .."
Either
executeQuery - The query is executed
executeQuery - Latitude=" ...
executeQuery - Query End
or
Error executeQuery and its stacktrace
AND AFTER THIS YOU WILL UNDERSTAND WHATS GOING ON.., while you have no log means that it is executing the query... firewalls ecc can change this time...
NOTE: Adapt the logger to your (android LOG), don't you Toast try to use the standard LOG.