我正在用 Hibernate 制作一个 Java 项目。这是我登录数据库的代码:
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 }
如果我使用了错误的用户名/密码,我会得到这个异常:
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)
无论我使用多少次 try/catch 和 throws,该方法仍然正确返回。我唯一的解决方法是添加这一行,但它更像是“黑客修复”而不是真正的修复。
Query q = session.createQuery("from Student");
q.list();
在上述代码的第 22 行和第 23 行之间。