Java SQL (JDBC) : how to move to the next column?

2019-08-10 01:10发布

问题:

I'm trying to check if the "Username" and "Email" arguments in my constructor are existed in the SQL Table.

this is my code:

    public DB(String usr, String eml, String pwd) {
    this.usr = usr;
    this.eml = eml;
    this.pwd = pwd;

    String jdbcUrl = "jdbc:mysql://localhost:3306/registered";
    String jdbcUser = "....";
    String jdbcPassword = "....";

    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(jdbcUrl, jdbcUser,
                jdbcPassword);
        statement = connection.createStatement();

now , if i use SELECT with two columns, like this:

String command = "SELECT UserName,Email FROM users WHERE UserName LIKE '" + this.usr.toString() + "';";
        resultSet = statement.executeQuery(command);

and then do my loop for resultSet... like this:

while (resultSet.next()) {

            if (usr.equalsIgnoreCase(resultSet.getString("UserName"))) {
                System.out.println("UserName : " + this.usr + " is taken!");

            }

            else if (eml.equalsIgnoreCase(resultSet.getString("Email"))) {
                System.out.println("Email : " + this.eml + " is taken!");

            }
            else {
                System.out.println("Email : " + this.eml + " and UserName : " + this.usr + " are AVAILABLE!");
                command = "INSERT users SET UserName = '" + this.usr.toString() + "',Email = '" + this.eml.toString() + "',Password = '" + this.pwd.toString() + "',Status = '0' ,Connected = '1';";        
                statement.executeUpdate(command);
    }
        }
    } catch (SQLException e) {
        System.out.println("SQLException: " + e.getMessage());
        System.out.println("Vendor error: " + e.getErrorCode());

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

the

resultSet.next()

only runs over the "FIRST" column which means if the "usr" exists in the table it works, but if the "usr" does not exist in the table, the other two if statements does-not work ..

,... i want to check both first column and second,.. and maybe third or more soon.. , any help?

回答1:

Your WHERE clause only tests for the UserName, so if the UserName doesn't match this.usr.toString(), the resultSet will be empty, so the while loop won't be entered.

You should change the query to match all the fields you care about - something like - "SELECT UserName,Email FROM users WHERE UserName = ... OR Email = ..."

If the resultSet is empty, you'll know that you can insert the new record. Otherwise, you can check which of the fields (UserName, Email) is already taken.

One more thing you should be aware of - executing a SQL statement without PreparedStatement makes your code vulnerable to SQL injection attacks.

You should change your code to something like this :

   PreparedStatement pstmt = con.prepareStatement("SELECT UserName,Email FROM users WHERE UserName = ? OR Email = ?");
   pstmt.setString(1, this.usr);
   pstmt.setString(2, this.eml);
   resultSet = pstmt.executeQuery();

You should change your INSERT statement similarly to use PreparedStatement.