I must be doing this all wrong, but I don't know quite how to make it do exactly what I want. I have three files:
RunMe.java - something to test my code with
UserDatabase.java - represents my database
DbCommunicator.java - used to do transactions
output
The checkCredentials() code works just fine. But I want to be able to retrieve the data from the database afterwards. I thought the UserDatabase was populated with values. Evidently, it's not working the way I expected it to. Can anyone help me understand where I'm going wrong and what I can do with my code to make it work?
RunMe.java - something to test my code with
package hib;
public class RunMe {
public static void main(String[] args) {
DbCommunicator db = new DbCommunicator();
//db.register("John", "Smith", "me", "qwerty", "[email protected]");
System.out.println(db.checkCredentials("user", "pass"));
System.out.println(db.getFirst());
System.out.println(db.getLast());
}
}
UserDatabase.java - represents my database
package hib;
public class UserDatabase {
private int id;
private String username;
private String password;
private String first;
private String last;
private String email;
private long attributes;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getAttributes() {
return attributes;
}
public void setAttributes(long attributes) {
this.attributes = attributes;
}
}
DbCommunicator.java - used to do transactions
package hib;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class DbCommunicator {
public int register(String first, String last, String username, String password, String email)
{
Session session = null;
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
// Find the next available ID number
int I = 0;
while(true)
{
List<?> list = session.createQuery("select db.id from UserDatabase db where db.id='" + I + "'").list();
Iterator<?> iter = list.iterator();
if(!iter.hasNext())
{
//int id = (Integer)iter.next();
System.out.println(I);
break;
}
I++;
}
// Check to see if the username is available
List<?> list = session.createQuery("select db.username from UserDatabase db where db.username='" + username + "'").list();
Iterator<?> iter = list.iterator();
if(!iter.hasNext())
{
session.flush();
session.close();
return 2; // Username already exists
}
Transaction tx = session.beginTransaction();
UserDatabase db = new UserDatabase();
db.setId(I);
db.setFirst(first);
db.setLast(last);
db.setUsername(username);
db.setPassword(password);
db.setEmail(email);
db.setAttributes(0);
session.save(db);
tx.commit();
}
catch(Exception e) {
e.printStackTrace();
session.flush();
session.close();
return 1; // Exception thrown
}
session.flush();
session.close();
return 0; // No errors!
}
public int checkCredentials(String username, String password)
{
Session session = null;
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
List<?> list = session.createQuery("from UserDatabase").list();
Iterator<?> iter = list.iterator();
UserDatabase db = null;
while(iter.hasNext())
{
db = (UserDatabase)iter.next();
if(username.equals(db.getUsername()))
{
if(password.equals(db.getPassword()))
{
session.flush();
session.close();
return 0;
}
else
{
// Password mismatch!
session.flush();
session.close();
return 1;
}
}
}
}
catch(Exception e) {
e.printStackTrace();
}
// Could not connect to database, or username is wrong
session.flush();
session.close();
return 2;
}
public int getId() {
UserDatabase db = new UserDatabase();
return db.getId();
}
public String getUsername() {
UserDatabase db = new UserDatabase();
return db.getUsername();
}
public String getPassword() {
UserDatabase db = new UserDatabase();
return db.getPassword();
}
public String getFirst() {
UserDatabase db = new UserDatabase();
return db.getFirst();
}
public String getLast() {
UserDatabase db = new UserDatabase();
return db.getLast();
}
public String getEmail() {
UserDatabase db = new UserDatabase();
return db.getEmail();
}
public long getAttributes() {
UserDatabase db = new UserDatabase();
return db.getAttributes();
}
}
output
0 null null
The checkCredentials() code works just fine. But I want to be able to retrieve the data from the database afterwards. I thought the UserDatabase was populated with values. Evidently, it's not working the way I expected it to. Can anyone help me understand where I'm going wrong and what I can do with my code to make it work?

