简体   繁体   中英

DB2 Database + verification

So Im new to databases and Im trying to learn the ropes. I have a DB2 database that Im getting familiar with. I was assigned a task where I need to write a method that does a search on the database. The search will take in two parameters, a username and a user id number. If the user and the user id number does not match or if one or the other turns out null then It needs to throw a error. If its valid then it will continue with spitting out information about the user.

I was told to use the findall() function or something similar to it. I was looking online and what I have found is examples that deal with like or ilike and im not sure how something like that will work in my situation. What would be a decent example of how I would start to go about this?

any help is appreciated. Ill post back if I make any progress.

note: Im using groovy/grails. Domain,Controller,View setup.

Is this some homework assignment from school?

findall() is usually a method in regular expressions which I don't think is relevant in here. If you have a SQL database, that means you have a RDBMS which uses SQL as query language. You need to learn about the SELECT command which can look daunting when you look the first the time to the manual but it's actually simple for your case. You need something like:

SELECT userfield1, userfield2,..
FROM   myusertable
WHERE  myusertable.username = 'uname' AND myusertable.userid = userid

uname and userid are your search parameters. Please note that this SQL query should be done with a PREPARED statement for security reasons.

When you run this query using your database library you get back an array of results which you have to analyze. If it is empty, no user found.

Edit: updated to take into account the use of hibernate

Hibernate uses HQL which is like SQL and has indeed a findAll method. See http://grails.org/doc/latest/ref/Domain%20Classes/findAll.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM