Java MySQL 获取最后一行

发布于 2024-12-11 20:45:25 字数 1250 浏览 0 评论 0原文

所以我的程序有问题。基本上到目前为止,该程序就像一个基于 MySQL 的聊天。它将消息存储在数据库中并读取它们。我在阅读方面遇到了问题。它现在所做的是每隔 5 秒重新读取数据库中的所有消息。我试图让它只读取新消息,但效果不太好。这是我的代码:

public static void readChat()
{
    try
    {
        MySQL.sqlConnect();
        try
        {
            Statement st = con.createStatement();
            ResultSet res = st.executeQuery("SELECT * FROM  table1");
            while (res.next())
            {
                if (lastLine < res.getInt("id"))
                {
                    String message = res.getString("message");
                    Gui.out.append(message + "\n");
                    lastLine = res.getInt("id");
                }
            }
        }
        catch (SQLException s)
        {   
            System.out.println("SQL code does not execute.");
        }
        MySQL.sqlDisconnect();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

我不知道如何提高效率。执行它需要太长时间。如果 id 是 23,我什至看不到消息出现,因为它需要超过 5 秒的时间。我添加了

if (lastLine < res.getInt("id"))

lastLine = res.getInt("id");

,努力让它只读取新消息,但它没有按预期工作。我认为它仍然是逐行执行的,只是没有在聊天中显示。一定有一个更简单的方法。

编辑:好的,所以我解决了看不到消息的问题(我忘记删除每 5 秒清除聊天的代码部分)。但是发送消息的时间还是比较长,大概3-4秒吧?

So I'm having issues with my program. Basically this far the program is like a MySQL based chat. It stores messages in database and reads them. I'm having problems with the reading. What it does right now is ever 5 seconds re-read all the messages in the database. I tried to make it read only the new messages but that's not working out too well. This is my code:

public static void readChat()
{
    try
    {
        MySQL.sqlConnect();
        try
        {
            Statement st = con.createStatement();
            ResultSet res = st.executeQuery("SELECT * FROM  table1");
            while (res.next())
            {
                if (lastLine < res.getInt("id"))
                {
                    String message = res.getString("message");
                    Gui.out.append(message + "\n");
                    lastLine = res.getInt("id");
                }
            }
        }
        catch (SQLException s)
        {   
            System.out.println("SQL code does not execute.");
        }
        MySQL.sqlDisconnect();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

I'm not sure how to make this efficient. It takes way too long to execute that. If the id is 23 I can't even see the messages appear because it takes longer than 5 seconds. I added the

if (lastLine < res.getInt("id"))

and

lastLine = res.getInt("id");

in my effort to make it read only the new messages but it did not work as expected. I think it still executed line by line, just doesn't show it in the chat. There's got to be an easier way.

EDIT: Alright, so I fixed the problem with not seeing messages, (I forgot to remove the part of the code that cleared the chat every 5 seconds). But it still takes a long time to send messages, about 3-4 seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖伴 2024-12-18 20:45:25

试试这个,你会想知道:)

ResultSet res = st.executeQuery("SELECT * FROM  table1 where id > "+lastLine);

instad

ResultSet res = st.executeQuery("SELECT * FROM  table1");

并得到结果,而不需要 if

       while (res.next())
        {
                String message = res.getString("message");
                Gui.out.append(message + "\n");
                lastLine = res.getInt("id");
        }

当然要确保 id 字段上的索引

Try this, and you will wonder :)

ResultSet res = st.executeQuery("SELECT * FROM  table1 where id > "+lastLine);

instad

ResultSet res = st.executeQuery("SELECT * FROM  table1");

and get result without if

       while (res.next())
        {
                String message = res.getString("message");
                Gui.out.append(message + "\n");
                lastLine = res.getInt("id");
        }

Of course make sure index on id field

千柳 2024-12-18 20:45:25

您可以只读取所需的行,当前您正在读取所有行,请尝试以下操作,

public static void readChat()
{
    try
    {
        MySQL.sqlConnect();
        try
        {
            Statement st = con.Preparestatment("SELECT * FROM  table1 where id > ?");
        st.setInt(0,lastLine);
            ResultSet res = st.executeQuery();
            while (res.next())
            {
                String message = res.getString("message");
                Gui.out.append(message + "\n");
                lastLine = res.getInt("id");
            }
        }
        catch (SQLException s)
        {   
            System.out.println("SQL code does not execute.");
        }
        MySQL.sqlDisconnect();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

you can just read the required rows, currently you are reading all the rows try following,

public static void readChat()
{
    try
    {
        MySQL.sqlConnect();
        try
        {
            Statement st = con.Preparestatment("SELECT * FROM  table1 where id > ?");
        st.setInt(0,lastLine);
            ResultSet res = st.executeQuery();
            while (res.next())
            {
                String message = res.getString("message");
                Gui.out.append(message + "\n");
                lastLine = res.getInt("id");
            }
        }
        catch (SQLException s)
        {   
            System.out.println("SQL code does not execute.");
        }
        MySQL.sqlDisconnect();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
我是男神闪亮亮 2024-12-18 20:45:25

您仍然迭代所有结果。如果您有某种方法来存储消息发送的时间,即创建时间列,您可以尝试通过执行

ResultSet res = st.executeQuery("SELECT * FROM  table1 WHERE id = ? AND time > ?");

第一个 ? 操作来缩短返回的结果数量。是用户的 id,第二个?是最后阅读消息的时间。这是基于 id 是某个唯一用户 ID 的假设。

You still iterate through all results. If you have some way of storing the time at which messages are sent i.e creating a time column, you can try to shorten how many results are returned by doing

ResultSet res = st.executeQuery("SELECT * FROM  table1 WHERE id = ? AND time > ?");

where the first ? is the id of the user and the 2nd ? is the time of the last read message. This is based off the assumption id is some unique user id.

月亮邮递员 2024-12-18 20:45:25

如何维护一个列表,在将其保存到数据库时向该列表添加新消息,然后从该列表中读取新消息并在 readChat() 末尾清除它?

What about maintaining a list, add new message to that list when you save it to the database and just read the new messages from that list and clear it at the end of readChat()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文