Java - 从MySQL数据库获取数据 [英] Java - Getting Data from MySQL database
问题描述
我已连接到一个MySQL数据库,其中包含四个字段(第一个是ID,后一个包含varchar字符串)。
I've connected to a MySQL database, which contains four fields (the first of which being an ID, the latter ones each containing varchar strings).
我试图获取数据库的最后一行,并检索字段的内容,以便我可以将它们设置为变量(一个int和三个字符串),并在以后使用。
I am trying to get the last row of the database and retrieve the contents of the fields so that I can set them to variables (an int and three strings) and use them later.
到目前为止,我有最低限度的连接,我从哪里去?正如你可以看到,我试图写一个SQL语句获得最后一行,但它都出了错,从那里,我不知道如何拆分成单独的字段。
So far, I have the bare minimum to make the connection, where do I go from here? As you can see I have tried to write a SQL statement to get the last row but it's all gone wrong from there and I don't know how to split it into the separate fields.
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/t", "", "");
Statement st = con.createStatement();
String sql = ("SELECT * FROM posts ORDER BY id DESC LIMIT 1;");
st.getResultSet().getRow();
con.close();
推荐答案
这里:
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/t", "", "");
Statement st = con.createStatement();
String sql = ("SELECT * FROM posts ORDER BY id DESC LIMIT 1;");
ResultSet rs = st.executeQuery(sql);
if(rs.next()) {
int id = rs.getInt("first_column_name");
String str1 = rs.getString("second_column_name");
}
con.close();
在 rs.getInt
或 rs.getString
,您可以从 1
开始传递 column_id
通过 column_name
作为它更丰富的信息,因为您不必查看数据库表
,其中 index
是列
。
In rs.getInt
or rs.getString
you can pass column_id
starting from 1
, but i prefer to pass column_name
as its more informative as you don't have to look at database table
for which index
is what column
.
UPDATE: rs.next
boolean next()
throws SQLException
boolean next() throws SQLException
将光标从当前位置向下移动一行。 A
ResultSet游标最初位于第一行之前;
首先调用方法next使第一行成为当前行;
第二次调用使第二行成为当前行。
Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
当对下一个方法的调用返回false时,光标位于
后的最后一行。任何调用ResultSet方法(
需要当前行)将导致抛出SQLException。如果
结果集类型是TYPE_FORWARD_ONLY,它是供应商指定的
他们的JDBC驱动程序实现将返回false还是抛出一个
SQLException,然后调用next。
When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown. If the result set type is TYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation will return false or throw an SQLException on a subsequent call to next.
如果为当前行打开了输入流,则调用方法
next将隐式关闭它。
If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.
返回:
如果新的当前行是有效的,则为true; false如果没有更多的行抛出:
SQLException - 如果发生数据库访问错误或在闭合结果集上调用此方法
Returns: true if the new current row is valid; false if there are no more rows Throws: SQLException - if a database access error occurs or this method is called on a closed result set
这篇关于Java - 从MySQL数据库获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!