开发者

Compare textfield value with database value

I need help in creating my application, I create a database and connect it to my application my database contain manager table having a id, name, etc of fields. when I enter any id in textfield it must 开发者_开发问答compare the value of textfield with database values how can I do this?


First learn SQL. It's the language which you're supposed to communicate with the DB in order to retrieve and store data. In this particular case, the key answer is the SQL WHERE clause.

SELECT id, name FROM manager WHERE name = "somename"

or if you want to select multiple values, e.g. name and password, use the AND:

SELECT id, name, password FROM manager WHERE name = "somename" AND password = MD5("somepass")

It'll return all matching results, which can be zero or one (or even more if you don't have your keys right).

Your next step is to learn JDBC. It's a Java API which enables you to execute SQL queries using Java code and obtain the results from the DB. In this particular case you'd like to use PreparedStatement to set user-controlled values in a SQL query and execute it.

preparedStatement = connection.prepareStatement("SELECT id, name, password FROM manager WHERE username = ? AND password = MD5(?)");
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
// ...

See also:

  • SQL tutorial
  • JDBC tutorial


Compare the id in textField, assuming it is String, using String#equalsIgnoreCase against the value of id retrieved from database using JDBC like shown here. Let me know in case you get stuck anywhere - Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜