我的结果集返回 0 条记录,但数据库具有该值,当在 mysql 中运行相同的 mysql

在这里我从文本字段中获取值


private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         


    String userName = txtUserName.getText();

    char[] passwordArray = txtPassword.getPassword();


    String password = " ";

    for(int i = 0; i < passwordArray.length; i++){

        password = password + passwordArray[i];

    }


    int numberOfRecords = 0;


    if(userName.equals("") || password.equals("")){

        JOptionPane.showMessageDialog(this, "Please fill the form properly:");

    }else{

        System.out.println("i am here");

        numberOfRecords = userServices.tryLogin(userName, password);

        System.out.println("after number of records");

        if(numberOfRecords == 1){

            this.dispose();

            mainForm.setVisible(true);

        }else{

            JOptionPane.showMessageDialog(this, "Sorry user name or password is incorrect:");

        }

    }

    txtUserName.setText(" ");

    txtPassword.setText("");

}

这是我的尝试登录方法


public int tryLogin(String userName, String password) {


    int numberOfRecords = 0;


    SQLQueryUtil sql = new SQLQueryUtil();

   sql.connect(false);

   String query = "SELECT COUNT(*) AS `number_of_records` FROM `user` "

            + "WHERE `userName`='" + userName + "' AND `password`='" + password + "';";

    System.out.println(query);


    ResultSet resultSet;


    try{


       resultSet = sql.executeQuery(query);

        resultSet.isBeforeFirst();


       numberOfRecords = resultSet.getRow();


         System.out.println("number of Records =" + numberOfRecords);

    } catch (Exception ex) {

        ex.printStackTrace();

    } finally{

        sql.disconnect();

    }

    System.out.println("i am here also..............................");

    return numberOfRecords;


}

当我点击登录按钮时,它显示以下结果驱动程序加载连接建立语句创建选择计数(*)number_of_records从user哪里userName=''和password='';记录数 =0


慕虎7371278
浏览 140回答 2
2回答

德玛西亚99

您正在用空格声明密码。只需删除密码声明中的空格并尝试 .String password = "";for(int i = 0; i < passwordArray.length; i++){&nbsp; &nbsp; password = password + passwordArray[i];}

小唯快跑啊

getRow() 方法检索当前行号,而不是行数。因此,在开始迭代 ResultSet 之前,getRow() 返回 0。所以尝试这种方式。resultSet.last();&nbsp;total = resultSet.getRow();resultSet.beforeFirst();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java