Hi,
Im setting the username and password for the hibernate.cfg.xml dynamically like the following code.
Code:
public class Sessions implements QueriesInterface{
private static SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
static {
try {
String userName = GuigenserviceImpl.userName;
String password = GuigenserviceImpl.password;
String hostName = GuigenserviceImpl.hostName;
String portNo = GuigenserviceImpl.portNo;
String sId = GuigenserviceImpl.sId;
// Create the SessionFactory from hibernate.cfg.xml
configuration
.setProperty("hibernate.connection.username", userName);
configuration
.setProperty("hibernate.connection.password", password);
configuration.setProperty("hibernate.connection.url",
connection_url + hostName + ":" + portNo + ":" + sId);
configuration.configure("/hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
sessionFactory.openSession();
GuigenserviceImpl.strAccpted = "true";
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
GuigenserviceImpl.strAccpted = "false";
ex.printStackTrace();
configuration = null;
sessionFactory = null;
throw new ExceptionInInitializerError(ex);
}
}
/**
* The session factory is created at class load time and is * provided here.
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
While the configuration.configure() executes, it is setting up my dynamic username and password to the configuration file.
Suppose if im giving wrong username and password means, it shows SQLException in console. But in the code, it is not going to catch block. The program is executing normally.
So can anyone tell me how to catch the exception thrown during the time of wrong username and password was given.
Also see my post regarding this topic on the following URL.
http://www.coderanch.com/t/439701/Object-Relational-Mapping/java/catch-exception-while-validating-username
Thanks in advance.