简体   繁体   English

SQL-如何通过一个SQL查询返回多行?

[英]SQL - how to return multiple rows with one SQL query?

I have a managed bean which makes SQL queries to Oracle database. 我有一个托管bean,它可以对Oracle数据库进行SQL查询。 This is just very simple example how I make SQL queries. 这只是我进行SQL查询的非常简单的示例。 This is the table structure: 这是表结构:

GLOBALSETTINGS
---------------------------------
SessionTTL          VARCHAR2(40 BYTE)
MAXACTIVEUSERS  NUMBER
ACTIVEUSERS         VARCHAR2(20 BYTE)

I use this table just to store application settings. 我使用此表只是为了存储应用程序设置。 In the example listed below I can fetch just one string with one SQL statement. 在下面列出的示例中,我可以使用一条SQL语句仅获取一个字符串。 I want with SQL query to fetch the content of the three rows - SessionTTL, MAXACTIVEUSERS, ACTIVEUSERS. 我想使用SQL查询来获取三行的内容-SessionTTL,MAXACTIVEUSERS,ACTIVEUSERS。 Is it possible? 可能吗?

   public String CheckUserDB(String userToCheck) throws SQLException {
        String storedPassword = null;        
        String SQL_Statement = null;

        if (ds == null) throw new SQLException();      
   Connection conn = ds.getConnection();
        if (conn == null) throw new SQLException();      

   try {
        conn.setAutoCommit(false);
        boolean committed = false;
            try {
                   SQL_Statement = "SELECT Passwd from USERS WHERE Username = ?";

                   PreparedStatement passwordQuery = conn.prepareStatement(SQL_Statement);
                   passwordQuery.setString(1, userToCheck);

                   ResultSet result = passwordQuery.executeQuery();

                   if(result.next()){
                        storedPassword = result.getString("Passwd");
                   }

                   conn.commit();
                   committed = true;
             } finally {
                   if (!committed) conn.rollback();
                   }
        }
            finally {               
            conn.close();

            }  

   return storedPassword;       
   }                         

PS I want the content of the rows. PS我想要行的内容。

I'm hoping I understand what you are asking for, but I fear I don't as it seems too simple, but anyway... 我希望我能理解您的要求,但我担心我似乎不太简单,但是无论如何...

I think you want the contents of 3 columns, not rows. 我认为您想要3列而不是行的内容。 And yes you can, you just specify the columns you want returned in your SQL statement: 是的,您可以在SQL语句中指定要返回的列:

SELECT SessionTTL, MAXACTIVEUSERS, ACTIVEUSERS FROM GLOBALSETTINGS WHERE (condition)...

you can also use * as a shortcut for all columns iof you don't want to explicitly specify them: 您也可以将*用作所有不想显式指定的列的快捷方式:

SELECT * FROM GLOBALSETTINGS WHERE (condition)...

Some background reading on SQL syntax might be useful 一些有关SQL语法的背景阅读可能会有用

If I read this correctly (sorry if mistaken), all you want to do is change your SQL command to select ALL COLUMNS in your database table. 如果我没看错(抱歉的话,对不起),您要做的就是更改您的SQL命令,以在数据库表中选择ALL COLUMNS。

To do so: 为此:

string SqlAll = @"SELECT Database.SessionTTL, Database.MAXACTIVEUSERS, Database.ACTIVEUSERS FROM Database";

This will retrieve ALL columns in the database. 这将检索数据库中的所有列。 You can also have conditional statements in your queries when you want to filter for logical reasons, such as TOP 20 to get the first 20 results from the result set. 当您出于逻辑原因要进行过滤时,也可以在查询中包含条件语句,例如TOP 20以从结果集中获取前20个结果。

如果您想通过一个sql查询返回多行,则可能需要循环查看ArrayList,在该循环中代码将遍历您的记录并匹配并找到所有可能的结果,直到记录列表的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM