如何用Java选择MySQL表的最后一行?

时间:2021-09-25 09:22:01

I am writing something for my friend that will involve storing some data in a database with a primary key which is an integer which will increment. For example:

我正在为我的朋友写一些东西,其中涉及将一些数据存储在数据库中,主键是一个将增加的整数。例如:

id    name      Age

1     James     15
2     Max       24
3     Jordan    61

How would I retrieve the integer under the id column in the last row? And this would be with Java. Thanks.

如何在最后一行的id列下检索整数?这将是Java。谢谢。

2 个解决方案

#1


2  

If the "last" row is the one with the highest ID, your SQL (to retrieve just the ID, like you asked) will look like:

如果“最后”行是具有最高ID的行,则您的SQL(仅检索ID,就像您要求的那样)将如下所示:

SELECT id FROM User ORDER BY id DESC LIMIT 1;

In Java, you'll probably be using JDBC; best practice is to learn how to use PreparedStatements.

在Java中,您可能正在使用JDBC;最佳实践是学习如何使用PreparedStatements。

#2


0  

is it like

是这样的

id name age

id名称年龄

1 james 15
2 max 24
3 jordan 61

PRIMARY KEY(id)

 SELECT id

FROM `tablename`

ORDER BY id DESC 

LIMIT 0 , 1

this will return the last row id result set from ur table

这将从ur表返回最后一行id结果集

#1


2  

If the "last" row is the one with the highest ID, your SQL (to retrieve just the ID, like you asked) will look like:

如果“最后”行是具有最高ID的行,则您的SQL(仅检索ID,就像您要求的那样)将如下所示:

SELECT id FROM User ORDER BY id DESC LIMIT 1;

In Java, you'll probably be using JDBC; best practice is to learn how to use PreparedStatements.

在Java中,您可能正在使用JDBC;最佳实践是学习如何使用PreparedStatements。

#2


0  

is it like

是这样的

id name age

id名称年龄

1 james 15
2 max 24
3 jordan 61

PRIMARY KEY(id)

 SELECT id

FROM `tablename`

ORDER BY id DESC 

LIMIT 0 , 1

this will return the last row id result set from ur table

这将从ur表返回最后一行id结果集