Reputation: 1
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?
Upvotes: 0
Views: 5369
Reputation: 16123
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!
Upvotes: 1
Reputation: 1108802
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();
// ...
Upvotes: 1