简体   繁体   English

为什么此查询返回NULL?

[英]Why does this Query return NULL?

I have a derby users database which I query, when the user clicks login on the application. 当用户单击应用程序上的登录名时,我有一个查询的derby用户数据库。

However, when I query the users table with the parameter [user] derby returns a null Object instead of the record it ought to return. 但是,当我使用参数[user] derby查询用户表时,derby返回一个空对象,而不是应该返回的记录。

Here is my code: 这是我的代码:

String ssql = "SELECT * FROM USERS WHERE UNAME LIKE ?";
   try{
       DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
       con = DriverManager.getConnection(url);
       sql = con.prepareStatement(ssql, Statement.RETURN_GENERATED_KEYS);
       sql.setString(1, cbox_chooseUser.getSelectedItem().toString());
       sql.executeQuery();
       ResultSet rs = sql.getGeneratedKeys();
           try{
              while (rs.next()) {
                 if(rs.getString("PW").toCharArray().equals(txt_password.getPassword())){
                     sql.close();
                     con.close();
                     return true;
                  }
             } catch (NPE ...) {...}
    }

I tried it multiple times wit a test user with both the pw and the username set to "test"; 我将pw和用户名都设置为“ test”的测试用户尝试了多次; but I always get the same error. 但我总是遇到同样的错误。 Why is the recordset always Null? 为什么记录集始终为Null?

Thanks for your help :) 谢谢你的帮助 :)

The documentation says 文件

ResultSet getGeneratedKeys() throws SQLException ResultSet getGeneratedKeys()引发SQLException

Retrieves any auto-generated keys created as a result of executing this Statement object. 检索由于执行此Statement对象而创建的任何自动生成的键。

If this Statement object did not generate any keys, an empty ResultSet object is returned. 如果此Statement对象未生成任何键,则返回一个空的ResultSet对象

Your select statement isn't generating any keys that's why it's returning an empty ResultSet. 您的select语句未生成任何键,这就是它返回空ResultSet的原因。 You aren't inserting anything hence no keys are being generated. 您不会插入任何内容,因此不会生成任何密钥。

You can try ResultSet rs = sql.executeQuery(); 您可以尝试ResultSet rs = sql.executeQuery(); . It should work. 它应该工作。

You are using it in wrong way. 您以错误的方式使用它。

The generated keys concept should be used only in the case DML of insert type query but not in the case of select query. 生成的键概念仅应在insert类型查询的DML情况下使用,而在select查询的情况下则不应使用。

select simply select the rows from the table. select只需从表中选择行。 In this case there is no chance of any keys getting generated. 在这种情况下,不会生成任何密钥。

In the case of insert query if any column is configured as auto increment or kind of functionality then some keys will get generated. insert查询的情况下,如果将任何列配置为auto increment或某种功能,则将生成一些键。 These keys can be caught using Statement.RETURN_GENERATED_KEYS in java. 可以使用Java中的Statement.RETURN_GENERATED_KEYS捕获这些键。

As you are using select query there is no need of using Statement.RETURN_GENERATED_KEYS . 在使用select查询时,无需使用Statement.RETURN_GENERATED_KEYS

You just modify below lines and everything will be fine. 您只需修改以下几行,一切都会好起来的。

sql = con.prepareStatement(ssql, Statement.RETURN_GENERATED_KEYS);
sql.setString(1, cbox_chooseUser.getSelectedItem().toString());
sql.executeQuery();
ResultSet rs = sql.getGeneratedKeys();

with

sql = con.prepareStatement( ssql );
sql.setString( 1, cbox_chooseUser.getSelectedItem().toString() );
ResultSet rs = sql.executeQuery();

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

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