从 Java 中的 SQL select 语句中获取一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26711383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting one value from SQL select statement in Java
提问by Tsar
I'm trying to return a value from a select statement. Its only one value because the value I'm returning is from the primary key column.
我正在尝试从 select 语句中返回一个值。它只有一个值,因为我返回的值来自主键列。
The SQL statement is SELECT itemNo FROM item WHERE itemName = 'astringvalue';
SQL 语句是 SELECT itemNo FROM item WHERE itemName = 'astringvalue';
My method for getting the value looks like this:
我获取值的方法如下所示:
private String viewValue(Connection con, String command) throws SQLException
{
String value = null;
Statement stmt = null;
try
{
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(command);
while (rs.next())
value = rs.toString();
}
catch (SQLException e )
{
e.printStackTrace();
}
finally
{
if (stmt !=
null) { stmt.close(); }
}
return value;
}
I do have a getConnection()
method too.
我也有getConnection()
方法。
Here is what im using to call the viewValue
method:
这是我用来调用该viewValue
方法的内容:
if((action.getSource() == btnSave) ||(action.getSource() == btnSavePrint) )
{
String findItemNoCommand = "SELECT itemNo FROM `item` WHERE itemName = '" + itemList.getSelectedItem() + "'";
try
{
itemNo = viewValue(conn, findItemNoCommand);
}
catch (SQLException e)
{
e.printStackTrace();
}
System.out.println(itemNo);
}
The code above was written for a ButtonHandler
上面的代码是为一个 ButtonHandler
Right now, for the println
I'm getting a "com.mysql.jdbc.JDBC4ResultSet@1e72cae
" .. I don't understand how it is so.. but I'm assuming that ResultSet is the wrong choice here.
现在,因为println
我得到了一个 " com.mysql.jdbc.JDBC4ResultSet@1e72cae
" .. 我不明白它是怎么回事.. 但我假设 ResultSet 在这里是错误的选择。
My question is.. what can i use there that can work?
我的问题是..我可以在那里使用什么可以工作?
Any help or clue as to what im doing wrong is much appreciated.
任何关于我做错了什么的帮助或线索都非常感谢。
采纳答案by Santhosh
Right now, for the println I'm getting a "com.mysql.jdbc.JDBC4ResultSet@1e72cae"
Right now, for the println I'm getting a "com.mysql.jdbc.JDBC4ResultSet@1e72cae"
is because you return the ResultSet
object here , value = rs.toString();
是因为你在ResultSet
这里返回对象,value = rs.toString();
From docs,
从文档,
A ResultSet object is a table of data representing a database result set, which is usually generated by executing a statement that queries the database
You access the data in a ResultSet object through a cursor. Note that this cursor is not a database cursor. This cursor is a pointer that points to one row of data in the ResultSet. Initially, the cursor is positioned before the first row. The method ResultSet.next moves the cursor to the next row. This method returns false if the cursor is positioned after the last row. This method repeatedly calls the ResultSet.next method with a while loop to iterate through all the data in the ResultSet.
ResultSet 对象是表示数据库结果集的数据表,通常通过执行查询数据库的语句生成
您可以通过游标访问 ResultSet 对象中的数据。请注意,此游标不是数据库游标。该游标是指向 ResultSet 中一行数据的指针。最初,光标位于第一行之前。ResultSet.next 方法将光标移动到下一行。如果光标位于最后一行之后,则此方法返回 false。此方法使用 while 循环重复调用 ResultSet.next 方法以遍历 ResultSet 中的所有数据。
You should tell the result set to get the value from the column ,
您应该告诉结果集从列中获取值,
value = rs.getString(1);
through index
通过索引
value = rs.getString("itemNo");
or through column name
或通过列名
回答by Jens
Change :
改变 :
value = rs.toString();
To:
到:
value = rs.getString(1);
rs.toString returns the result of the toString
method of the Object ResultSet
.
rs.getString(1)
gives you the first parameter of the resultset as a String
.
rs.toString 返回toString
Object 方法的结果ResultSet
。
rs.getString(1)
为您提供结果集的第一个参数作为String
.
回答by Kayaman
Use rs.getInt(1)
or rs.getString(1)
to retrieve the actual value from the ResultSet
. Then read a JDBC tutorial.
使用rs.getInt(1)
或rs.getString(1)
从 中检索实际值ResultSet
。然后阅读 JDBC 教程。
回答by Prabhakaran Ramaswamy
You need to change
你需要改变
while (rs.next())
value = rs.toString();
to
到
while (rs.next())
value = rs.getString("itemNo");
回答by AsSiDe
Try This:
尝试这个:
private String viewValue(Connection con, String command) throws SQLException
{
String value = null;
Statement stmt = null;
try
{
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(command);
while (rs.next())
value = rs.getString(1);
}
catch (SQLException e )
{
e.printStackTrace();
}
finally
{
if (stmt !=
null) { stmt.close(); }
}
return value;
}
回答by sonal
change it to
将其更改为
if(rs.next())
and
和
rs.getString("itemNo");
works !!!
工作!!!