从Java中的SQL select语句获取一个值 [英] Getting one value from SQL select statement in Java

查看:362
本文介绍了从Java中的SQL select语句获取一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从select语句返回一个值.它唯一的一个值,因为我要返回的值来自主键列.

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.

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;

    }

我也有一个getConnection()方法.

这是即时通讯用来调用viewValue方法的原因:

Here is what im using to call the viewValuemethod:

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);
        }

上面的代码是为ButtonHandler

现在,对于println,我得到的是"com.mysql.jdbc.JDBC4ResultSet@1e72cae" ..我不知道这是怎么回事..但是我假设ResultSet是这里的错误选择.

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.

我的问题是..在那里可以使用什么呢?

My question is.. what can i use there that can work?

任何关于我做错了什么的帮助或线索都将受到赞赏.

Any help or clue as to what im doing wrong is much appreciated.

推荐答案

Right now, for the println I'm getting a "com.mysql.jdbc.JDBC4ResultSet@1e72cae"

是因为您在此处返回ResultSet对象,value = rs.toString();

is because you return the ResultSet object here , value = rs.toString();

来自文档

ResultSet对象是代表数据库结果的数据表 集,通常是通过执行查询以下内容的语句生成的 数据库

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

您可以通过游标访问ResultSet对象中的数据.注意 此游标不是数据库游标.该光标是一个指针 指向ResultSet中的一行数据.最初,光标是 位于第一行之前.方法ResultSet.next将 光标到下一行.如果光标是 位于最后一行之后.此方法反复调用 带有while循环的ResultSet.next方法可遍历所有 数据在结果集中.

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.

您应该告诉结果集从列中获取值,

You should tell the result set to get the value from the column ,

value = rs.getString(1);

通过索引

value = rs.getString("itemNo");

或通过列名

这篇关于从Java中的SQL select语句获取一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆