0

I'm making a Java project with Hibernate. This is my code for logging on the database:

1    public static void connect(String username, String password) throws Exception
2    {
3        Configuration cfg = new Configuration();
4        cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
5        cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
6        cfg.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/Scheme");
7        cfg.setProperty("hibernate.connection.username", username);
8        cfg.setProperty("hibernate.connection.password", password);
9
10        cfg.addResource("model/Classroom.hbm.xml");
11        cfg.addResource("model/Classteacher.hbm.xml");
12        cfg.addResource("model/Education.hbm.xml");
13        cfg.addResource("model/Lesson.hbm.xml");
14        cfg.addResource("model/Loginauth.hbm.xml");
15        cfg.addResource("model/Schoolclass.hbm.xml");
16        cfg.addResource("model/Semester.hbm.xml");
17        cfg.addResource("model/Semestersubject.hbm.xml");
18        cfg.addResource("model/Student.hbm.xml");
19        cfg.addResource("model/Subject.hbm.xml");
20        cfg.addResource("model/Teacher.hbm.xml");
21        cfg.addResource("model/Login.hbm.xml");
22        sessionFactory = cfg.buildSessionFactory();
23    }

If I use the wrong username/password I get this exception:

java.sql.SQLException: Access denied for user 'root1'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:190)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at util.HibernateUtil.connect(HibernateUtil.java:22) <-- Line 22 at the above code
at core.LoginWindow$5.run(LoginWindow.java:199)

No matter how many try/catch and throws I use, the method still returns correctly. The only fix I have is to add this line, but it's more of a "Hack fix" than a real fix.

Query q = session.createQuery("from Student");
q.list();

Between line 22 and 23 in the above code.

René Jensen
  • 451
  • 3
  • 20
  • 2
    Actually a test-query isn't a dirty hack at all. Though it could be more general, such as SELECT 1. I'd suggest you externalize the config to a config file and add some pooling mechanism such as C3P0 – heikkim Nov 19 '12 at 11:24
  • @heikkim The "select 1" works as well. Although it still throws the uncatchable exception. The program still runs afterwards, so I guess it doesn't matter. I used the Configuration object as I didn't want the username and password to be in plain text files packed in the .Jar in the end. And I'll look into the C3P0. – René Jensen Nov 19 '12 at 11:31
  • Which exception you caught ? – someone Nov 19 '12 at 12:14
  • 1
    If you can't catch the Exception it sounds like it is being caught and logged elsewhere and not being rethrown. Besides that I am reading that buildSessionFactory() is deprecated, take a look at this http://stackoverflow.com/questions/8621906/is-buildsessionfactory-deprecated-in-hibernate-4 for alternatives. – Dave Richardson Nov 19 '12 at 14:09
  • if there is any problem with your database configuration properties, the exception is generated at buildSessionFactory(), the last line of your method. May be thats why you think the method is returning correctly. – Sazzadur Rahaman Nov 19 '12 at 14:19

1 Answers1

0

The exception thrown was indeed not a "Not-caught" exception, but instead just logging it somewhere I couldn't reach. It was missing this part:

Exception in thread "AWT-EventQueue-0"

I was using Hibernate 3, but upgraded to 4. Then changed the buildSessionFactory() method to this: Is buildSessionFactory() deprecated in hibernate 4?. Now I don't get a logged exception if I supply the wrong credentials.

Problem solved, love you all.

Community
  • 1
  • 1
René Jensen
  • 451
  • 3
  • 20