Java MySQL 获取最后一行
所以我的程序有问题。基本上到目前为止,该程序就像一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个,你会想知道:)
instad
并得到结果,而不需要 if
当然要确保 id 字段上的索引
Try this, and you will wonder :)
instad
and get result without if
Of course make sure index on id field
您可以只读取所需的行,当前您正在读取所有行,请尝试以下操作,
you can just read the required rows, currently you are reading all the rows try following,
您仍然迭代所有结果。如果您有某种方法来存储消息发送的时间,即创建时间列,您可以尝试通过执行
第一个 ? 操作来缩短返回的结果数量。是用户的 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
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.
如何维护一个列表,在将其保存到数据库时向该列表添加新消息,然后从该列表中读取新消息并在 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()?