简体   繁体   English

ResultSet 总是返回 null,不管 JDBC/MySQL、Java 有什么变化

[英]ResultSet always returns null, no matter what the changes are in JDBC/MySQL, Java

I have an application with a login form.我有一个带有登录表单的应用程序。 What I want is simply retrieve the credentials of the user who logs in, ie select the username and password from the login form and make a simple select query.我想要的只是检索登录用户的凭据,即从登录表单中选择用户名和密码并进行简单的选择查询。 However the resultset always return null, no matter what changes I made.但是,无论我做了什么更改,结果集始终返回 null。 I would like to ask what could be the problem?我想问一下可能是什么问题? For the purpose of testing I have made 2 static variable in which I store the retrieved username and password (I will made the other alongside these fields in 1 class User but after I fix this).出于测试的目的,我创建了 2 个静态变量,我将检索到的用户名和密码存储在其中(我将在 1 类用户中的这些字段旁边创建另一个,但在我修复此问题后)。 Here is the code: /The fields in the database are exactly like the ones written in the brackets of the getString() method/代码如下:/数据库中的字段和getString()方法括号里写的一模一样/

package graphical_interface;
import business_logic.User;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Controller {
    @FXML
    public Button loginB;
    public Button exitB;
    public TextField userinp;
    public TextField passinp;
    public Parent pr;
    public static String s1 = null;
    public static String s2 = null;
    @FXML
    private void closeApp() {
        Platform.exit();
    }
    String username = null;
    String password = null;
    @FXML
    private void checkB(){

            try {
                PreparedStatement pstmt2 = null;
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://remotemysql.com/CXcocPWj6l", "CXcocPWj6l", "czNrEV9umD");
                String query  = "select * from User where Username = ? and PassWord = ?";
                pstmt2.setString(1,userinp.getText());
                pstmt2.setString(2,passinp.getText());
                ResultSet valueExist = pstmt2.executeQuery(query);
                if(valueExist.next()){
                    s1 = valueExist.getString("Username");
                    s2 = valueExist.getString("PassWord");
                   // System.out.println(valueExist.getString("PassWord");
                }
                conn.close();
                System.out.println(s1);
                System.out.println(s2);

            } catch (Exception exp){
                System.out.println(exp.getLocalizedMessage());
            }
    }
}

The SQL syntax seems to be correct, so few questions: are you sure that the table contains the record you are finding for? SQL 语法似乎是正确的,所以问题很少:您确定该表包含您要查找的记录吗? Are you sure that "userinp.getText()" and "passinp.getText()" return something not null?您确定“userinp.getText()”和“passinp.getText()”返回的不是空值吗?

Finally two advice: use try with resource or at least use the finally block.最后两个建议:对资源使用 try 或至少使用 finally 块。

Replace the columnLabel with columnIndex ie replace用 columnIndex 替换 columnLabel 即替换

if(valueExist.next()){
    s1 = valueExist.getString("Username");
    s2 = valueExist.getString("PassWord");
}

with

if(valueExist!=null && valueExist.next()){
    s1 = valueExist.getString(columnIndex-of-Username);
    s2 = valueExist.getString(columnIndex-of-PassWord);
}

where columnIndex-of-Username and columnIndex-of-PassWord are integer values holding the columnIndex of Username and PassWord respectively.其中columnIndex-of-UsernamecolumnIndex-of-PassWord是分别UsernamePassWord的 columnIndex 的整数值。

Note : the first column is 1, the second is 2, ...注意:第一列是 1,第二列是 2,...

If you have any typo in columnLabels, it may help fix your problem.如果您在 columnLabels 中有任何拼写错误,它可能有助于解决您的问题。 Using columnIndex instead of columnLabel has another advantage of performance improvement (check https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzaha/jdbcperf.htm )使用columnIndex而不是columnLabel具有性能改进的另一个优势(检查https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzaha/jdbcperf.htm

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

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