MySQL ResultSet not relfecting new data in JDBC
Posted by: Dhanya R
Date: April 17, 2011 04:20AM

Hi all,
I have a simple program that queries for rows in a specific table at an interval. Using Mysql command line tool, I am inserting rows to the table. But with the program I have, it does not show the new rows. It always shows the rows which it could find when it executed the SQL for first time. I am doing commit properly. Also note that I could see insert in all other mysql clients and not in this simple JDBC program.
  In the following program, the count does not change unless I restart ti.

        Connection conn1 = null;
        try {
            String userName = "ms";
            String password = "ms";
            String url = "jdbc:mysql://192.168.0.100:3306/ms";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn1 = DriverManager.getConnection(url, userName, password);
            conn1.setAutoCommit(false);
            while (true) {
                Thread.sleep(3000);
            
                Statement stmt = conn1.createStatement();    
                ResultSet rs = stmt.executeQuery("SELECT * FROM MS;");
                int count = 0;
                while (rs.next()) {
                    count++;
                }
                rs.close();
                System.out.println("Count is " + count);
            }


        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Cannot connect to database server");
        }

Thanks
DM

Options: ReplyQuote


Subject
Written By
Posted
MySQL ResultSet not relfecting new data in JDBC
April 17, 2011 04:20AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.