how to check if a data exist on a table using hibernate - mysql

im using hibernate with my jsp page and mySQL ,how can i do that select * from student wher userName = *** with HQL
and how i chek if that username exist in 'Student' table ?
in my sql i use that
ResultSet resultat = statement.executeQuery();
if (resultat.next()) { ....}
i try this
Session hibernateSession = MyDB.HibernateUtil.currentSession();
hibernateSession.find("select xxx from Etudinat where p.Nom=xxxx");
its give me a list but
i have a login form send me a username and password
i want to chek if that username exist in the table Student to set the user on a session
what is the safty way to do that

I could not paste code into the comment. Try the following HQL:
from Etudinat where Nom = 'xxxx'
Even better, pass the username as a parameter.
Per OP request, code snippet based on the comments:
Query q = hibernateSession.createQuery("from Etudinat where Nom = :username");
q.setParameter("username", xxxx);
Etudinat e = q.uniqueResult();

Related

Hibernate query for employee with date

This is my code.. Query working when i search with mysql query(find it in screenshot).But failed with hiberbate query.
EmployeeAttendanceMaster masterEmployeeFromRepository = masterEmployeeRepository.findById(employee, date);
if (masterEmployeeFromRepository == null) {
System.out.println("SignIn Successfully");
}else System.out.println("You are already Logged In");
masterEmployeeRepository:
#Query("select me from EmployeeAttendanceMaster me where me.employee = ?1 and Date(me.date) = ?2 order by me.date desc")
EmployeeAttendanceMaster findById(Employee employee,Date date);
mysql db screenshot in with same query
Data with same date there in db..So it shoudnot go through if condition.It should follow else condition.But as long as i tried this it prints "SignIn Successfully"
Thanks advance
You are using the Spring Data JPA #Query annotation (as discerned from the full data type you have provided in the comments to your question). The query you specify with #Query (select me from EmployeeAttendanceMaster me ...) must be a valid JPA Query Language (JPQL) statement. From what I know and remember, JPQL does not have a Date() function. So, your query is invalid because it contains Date(me.date) which refers to a non-existent JPQL Date() function, even if you can run it directly on MySQL.
You can change your query declaration to:
#Query(value = "select * from EmployeeAttendanceMaster where employee_id = ?1 and Date(date) = ?2 order by date desc", nativeQuery = true)
This will force the JPA provider (Hibernate in your case) to treat the query as a native SQL query and will be executed on the underlying database without any translation. You will lose database independence though.

Select count return 0 mysql JDBC

I'm trying to store messages, so i create 2 tables in mysql with HeidiSql, the first one is msg and the second one is users. I wrote a store procedure to get the number of messages that have as sender the first parameter, as recipient the second parameter and viceversa, i stored this value in id variable. I need the variable to count the messages between 2 people. When i run the application from java, i always get id=0. I can't understand the mistake.
...
sql="call getIdMsg(?,?,?)";
sql2="call addRecord(?,?,?,?)";
try (Connection conn= DriverManager.getConnection(db_url,username,password);
CallableStatement statement = conn.prepareCall(sql);
CallableStatement statement2 = conn.prepareCall(sql2)){
statement.setInt(1,sender);
System.out.println(sender);
statement.setInt(2,recipient);
System.out.println(recipient);
statement.registerOutParameter(3, Types.INTEGER);
statement.execute();
id=statement.getInt(3);
System.out.println("ID is: "+id);
statement2.setInt(1,sender);
statement2.setInt(2,recipient);
statement2.setInt(3,id);
statement2.setString(4,msg);
statement2.execute();
System.out.println("The record is been added");
}
catch (SQLException sqle){sqle.printStackTrace();}
...
This is my sql code:
BEGIN
set #id = ((select count(*) from msg WHERE sender = #thisrecipient and recipient = #thissender)+(SELECT COUNT(*) FROM msg WHERE sender = #thissender and recipient = #thisrecipient));
END
These are my parameters:
thissender INT IN
thisrecipient INT IN
id INT OUT
Could you help me please?

HQL Query fails in Hibernate MySQL

I want to recuperate all rows from user table.
String queryS = "select u from user u";
System.out.println("entityManager: "+(entityManager == null));
Query query = entityManager.createQuery(queryS);
//staff
The line that throws the exception is Query query = entityManager.createQuery(queryS);
I don't know why even persistance file is ok and the table exists
The stack is:
10:36:06.693 [AWT-EventQueue-0] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
10:36:06.693 [AWT-EventQueue-0] DEBUG o.h.hql.antlr.HqlSqlBaseWalker - select << begin [level=1, statement=select]
You have to put the name of the table in the query as in the persistence file not in database.
If this:
String queryS = "select u from user u"
Is referred on table name you can't use createQuery method but createNativeQuery
If you want to use createQuery you must use in your query the entity/class mapped youe user table
Summarizing:
Case 1 (use createNativeQuery)
String queryS = "select u from user u";
Query query = entityManager.createNativeQuery(queryS);
Case 2 (use createQuery)
String queryS = "select u from " + User.class.getName() + " u";
Query query = entityManager.createNativeQuery(queryS);

Zend Framework - and in where clause

I am working on Zend Framework, got 1 problem in query, i want to fetch userid from tbl_user & want to check record with And in where clause
here is my query
$sql=$this->select()->from(users)->where('email = ? ', trim($email) , 'password = ?', md5(trim($password)) );
when i am printing query it prints
select * from users where email = 'test#gmail.com';
i want to print query like
select * from users where email = 'test#gmail.com' AND password='123456';
thanks in advance
You were close. You just need to call the where() method twice:
$this->select()->from(users)->where('email = ?', trim($email))
->where('password = ?', md5(trim($password)));

How to use Boolean logic in a mySQL SELECT query

How can I check if email = '$e' or username = '$e' inside my MySQL query.
Here is my MySQL query so far.
"SELECT user_id FROM users WHERE (email = '$e' AND pass=SHA1('$p'))"
If you want to modify you existing query so that it works even if $e matches username, you can do:
SELECT user_id
FROM users
WHERE (email = '$e' OR username = '$e') AND pass=SHA1('$p')