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

↘锁芯ラ 提交于 2020-01-05 12:23: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.



来源:https://stackoverflow.com/questions/24337166/java-sql-jdbc-how-to-move-to-the-next-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!