【问题标题】:Trying to search database for users with the user's first name尝试在数据库中搜索具有用户名的用户
【发布时间】:2021-02-08 06:44:25
【问题描述】:

我正在尝试使用数据库中第二列的名字来搜索数据库中的用户,但每次我尝试它时,它都会告诉我即使它存在于 db 中也找不到具有此用户名的用户

String usernamechk = txtflightname.getText();//txtflightname is the textfield to type the username to search for
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/airline","root","");
            pst = con.prepareStatement("select * from flights where 'usernamechk' = ?");
            pst.setString(1,usernamechk);
            ResultSet rs = pst.executeQuery();
            if(rs.next() == false)
            {
                JOptionPane.showMessageDialog(this, "user with this username not Found");
            }

【问题讨论】:

  • 尝试将 txtflightname.getText() 替换为 txtflightname.getText().toString().trim()

标签: java mysql database


【解决方案1】:

我将首先检查您从txtflightname.getText(); 提取的值是否正确。 尝试System.out.println(usernamechk) 并检查文本中是否有空格。您可以像这样从中删除空格:usernamechk.trim()。最后一件事,从您的查询中删除这些 ' ' 并像这样制定它:

select * from flights where usernamechk = ?

【讨论】:

    猜你喜欢
    • 2020-04-05
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多