Is there any other way to properly close the connection to ms access database at the end of the infinite loop? Because if a record is inserted into the table, with my code below I can't see the new row/rows inserted. It looks like that the database is not closing corectly or something...no idea why it is acting like this. If I manually close or open the database (with my program still running in the background) everything is ok - the new row/rows will appear in my query.
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws RemoteException,
InterruptedException, SQLException {
while (true) {
Connection con = DBConnection.getDBConnection();
System.out.println("Connection OK!");
Statement s = null;
try {
ResultSet rs = null;
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
con.setAutoCommit(true);
rs = s.executeQuery("SELECT Tel, Msg, Procesat FROM RcvMsg WHERE Procesat = 'NOK'");
while (rs.next())
{
String pn = rs.getString(1);
String str = rs.getString(2);
//do something
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
s.close();
con.close();
}
Thread.sleep(5000);
}
}
}
while (true)loop. If you are using an older version of UCanAccess then try upgrading to the latest version.