嵌入式 H2 数据库连接错误 - 用户名和密码错误
Posted
技术标签:
【中文标题】嵌入式 H2 数据库连接错误 - 用户名和密码错误【英文标题】:Embedded H2 Database Connection Error - wrong username and password 【发布时间】:2016-04-06 10:20:10 【问题描述】:我正在为 Hibernate、JavaEE 持久性和嵌入式 H2 数据库而苦苦挣扎。
我的 persistence.xml 中有以下部分:
<persistence-unit name="iot_pu">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:/tmp/databases/iot;AUTO_SERVER=TRUE;MVCC=true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
我得到以下堆栈跟踪:
javax.persistence.PersistenceException: Unable to build entity manager factory
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:66)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at iot.utils.AbstractTestSuite.setUpClass(AbstractTestSuite.java:22)
at iot.utils.AbstractTestSuite.getEmf(AbstractTestSuite.java:37)
at iot.utils.AbstractTest.init
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
Caused by: org.h2.jdbc.JdbcSQLException: Falscher Benutzer Name oder Passwort
Wrong user name or password [28000-191]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.engine.Engine.validateUserAndPassword(Engine.java:333)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:161)
at org.h2.engine.Engine.createSession(Engine.java:136)
at org.h2.engine.Engine.createSession(Engine.java:28)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:349)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:107)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:91)
at org.h2.Driver.connect
如果我用 IntelliJ 测试连接,它会说连接没有问题 - 没有用户名或密码。
导致问题的那一行 (AbstractTestSuite.java:22) 说
Persistence.createEntityManagerFactory("iot_pu");
是不是我忘记了要通过 Maven 导入的东西?
.........
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
.........
【问题讨论】:
我认为您在连接 url 中遗漏了用户名和密码 【参考方案1】:您的persistence.xml
中缺少hibernate.connection.username
和hibernate.connection.password
属性,或者您可以将它们直接插入您的连接URL(例如:"jdbc:oracle:thin:user/password@127.0.0.1:1521:SMIDVINT"
)。
【讨论】:
我已经添加了,但是还是有同样的错误。我也尝试了默认用户名“sa”...但它不起作用【参考方案2】:测试您的连接以确保您的设置正确:
/**
* Connect to H2db.
* calling example: connect("jdbc:h2:/Resources/databases/dbName", "JohnD", "secret")
*/
public static Connection connect (String dbPath, String userName, String password)
throws ClassNotFoundException, SQLException, JdbcSQLException
Class.forName("org.h2.Driver");
String dbURL= dbPath + ";IGNORECASE=TRUE;MODE=mysql;IFEXISTS=TRUE";
return DriverManager.getConnection(dbURL,userName,password);
【讨论】:
以上是关于嵌入式 H2 数据库连接错误 - 用户名和密码错误的主要内容,如果未能解决你的问题,请参考以下文章