Question : Is my hibernate code the equivalent of my SQL code?

I was using Java and SQL to access my database but I am now trying to use hibernate. I have attached my hibernate code and my code using SQL. Should they do the same?

My fear is that my hibernate code will have an error. If the the search is performed and it finds no entry will there be and error or will it simply not update user as in below,

query.setString("Username", client.getUsername());
            user = (Client) query.uniqueResult();

If user remains a null object thats fine.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
SQL Code

public String CheckLogin(String Username, String Password) throws RemoteException
{
	String answer = null;
	
	try
	{
	
	statement = link.createStatement();
	results = statement.executeQuery("SELECT * FROM USERS WHERE USERNAME ='"+Username+ "'");
	if (results.next()) {
		String dbpassword = results.getString("PASSWORD");
		String bdtype = results.getString("ACCOUNT_TYPE");
		if (dbpassword.equals(Password))
			{
				answer = "User Found";
			}
		else {
			answer= "Password Wrong";
	}}
	else {
		answer = "Username and/or Password Wrong";
	} }
	
	catch (Exception e) {
	System.out.println("Exception occured ---" + e);
}
	return answer;
	}}


Hibernate Code

public static Client checkLogin(Session session,ClientDTO client){
		String answer = null;
		Client user = new Client();

		String queryString = "from NMEAData where Username = :Username";
		Query query = session.createQuery(queryString);
		query.setString("Username", client.getUsername());
		user = (Client) query.uniqueResult();
		if (client.getUsername().equals(user.getUsername()))
		{
		if (client.getPassword().equals(user.getPassword()))
		{
			answer = "User Found";
			System.out.println(answer);
		}
	else {
		answer= "Password wrong";
		System.out.println(answer);
}}
else {
	answer = "Username and/or password incorrect";
	System.out.println(answer);
}
		System.out.println(user.getUsername());
		return user; 
		
		
	
}

Answer : Is my hibernate code the equivalent of my SQL code?

you'll get a NPE as it returns null
need to check for null


user = (Client) query.uniqueResult();
if (user!=null &&
   client.getUsername().equals(user.getUsername()) &&
   client.getPassword().equals(user.getPassword()))
{
                  answer = "User Found";
                  System.out.println(answer);
}
Random Solutions  
 
programming4us programming4us