【问题标题】:Updating an SQL database between queries?在查询之间更新 SQL 数据库?
【发布时间】:2017-09-19 17:07:03
【问题描述】:

我对正在运行的代码有疑问。它旨在根据从 SQL 数据库中检索到的变量值更新显示。当我运行代码并向表中插入另一个条目时,打印表值的循环不会改变。我不确定是因为表中的指针没有移动还是因为代码不允许动态更新。当我终止代码并重新运行时,会显示新数据。我正在使用 Eclipse。

import java.awt.Graphics;
import javax.swing.JFrame;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Tutorial1 extends JFrame 
{
    static String State; 
      public Tutorial1()
      {
             setTitle("Tutorial1");
             setSize(900, 900);
             setVisible(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
      }

      public void paint(Graphics g)
      {


          if (State.equals("0"))
          {
          g.drawRect(480, 480, 200, 100);
          }

          if (State.equals("1"))
          {
             g.fillRect(240, 240, 200, 100);
          }


      }


       public static void main(String[] args)
       {

           Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            try {
                new com.mysql.jdbc.Driver();
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                    String connectionUrl = "jdbc:mysql://localhost:3306/capstone";
                String connectionUser = "root";
                String connectionPassword = "root";
                conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
                stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                rs = stmt.executeQuery("SELECT id,Name,State FROM Sensors ORDER BY id DESC");

                {       

                while (rs.next()) {


                    Tutorial1 t = new Tutorial1();
                    //t.paint(null);

                     while (true) 
                    {

                         rs.refreshRow();
                         rs.updateRow(); 


                         String id = rs.getString("id");
                            String Name = rs.getString("Name");
                             State = rs.getString("State");
                            System.out.println("ID: " + id + ",Name: " + Name
                                    + ", State: " + State);

                            id = "id+1"; 

                        System.out.println("Success");
                        System.out.println(State);

                        t.repaint();

                    }

                }


            }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {




                //try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            //  try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
                //try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
            }            
       } 
}

【问题讨论】:

  • 如果您指的是 while(true) 循环不更新值,那么这是正确的行为,因为已经从数据库中检索了结果。如果您需要从数据库中获取新结果,则需要再次查询数据库。
  • while (true) 的表情会像疯了一样旋转。你真的应该限制它,否则你可能会有数千次重绘,并且有陈旧的数据排队。
  • @tadman 什么是限制循环每 10 秒执行一次的有效方法?
  • @J_D 我每次都相信“rs”。运行它会连接到数据库

标签: java mysql eclipse resultset


【解决方案1】:

所以,你的代码实际上做了如下:

  1. 您使用以下语句执行查询:

    rs = stmt.executeQuery("SELECT id,Name,State FROM Sensors ORDER BY id DESC");

    现在您的resultset 保存查询结果,resultset 中的第一行是id 最高的行。

  2. 那你去resultset的第一行:

    while (rs.next())

  3. 之后,您会在无限循环中不断刷新该特定行

    while (true) { rs.refreshRow(); ...

您永远不会移动到下一行(永远不会再调用 rs.next()),也永远不会在表中看到任何新插入的行(您不会刷新 整个 resultset - 只是当前行

当然,当您停止程序并再次运行 - 查询执行时,它会从表中读取新值,第一行具有较高的 id,因此您会在屏幕上看到预期的结果。

这就是为什么最好的办法是关闭结果集。并再次执行查询 - 如果您想从表中获取新值。

此外,正如 cmets 中的 tadman 所述 - 您应该在线程上使用 sleep 在重新查询之间暂停。

【讨论】:

  • @DavidCamille 我现在实际上注意到了一些东西——这让我想到一个问题——你的Sensors 表中有多少条目?因为您将其视为只有 一个 条目正在不断更新...如果不是这种情况,那么可能是您的问题的根源...另外tadman 的担忧是合理的……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多