我在尝试通过hibernate连接时遇到上述错误。但是,当我通过jdbc驱动程序连接时,一切正常。所以连接字符串不是问题。请指导我完成此错误。有任何我遗失的jar文件请告诉我
type Exception report
message org.hibernate.exception.SQLGrammarException: Cannot open connection
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.RuntimeException: org.hibernate.exception.SQLGrammarException: Cannot open connection
model.LoginBean.authorized(LoginBean.java:41)
controller.ShopServlet.processLoginPost(ShopServlet.java:68)
controller.ShopServlet.doPost(ShopServlet.java:55)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.hibernate.exception.SQLGrammarException: Cannot open connection
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:424)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1538)
org.hibernate.loader.Loader.doQuery(Loader.java:661)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
org.hibernate.loader.Loader.doList(Loader.java:2211)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
org.hibernate.loader.Loader.list(Loader.java:2090)
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
model.LoginBean.authorized(LoginBean.java:31)
controller.ShopServlet.processLoginPost(ShopServlet.java:68)
controller.ShopServlet.doPost(ShopServlet.java:55)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.sql.SQLException: Syntax error or access violation message from server: "Unknown database 'root'"
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2001)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1907)
com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:2524)
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:818)
com.mysql.jdbc.Connection.createNewIO(Connection.java:1808)
com.mysql.jdbc.Connection.<init>(Connection.java:452)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:421)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1538)
org.hibernate.loader.Loader.doQuery(Loader.java:661)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
org.hibernate.loader.Loader.doList(Loader.java:2211)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
org.hibernate.loader.Loader.list(Loader.java:2090)
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
model.LoginBean.authorized(LoginBean.java:31)
controller.ShopServlet.processLoginPost(ShopServlet.java:68)
controller.ShopServlet.doPost(ShopServlet.java:55)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 0 :(得分:0)
此错误可能是因为数据库名称不正确。你能检查一下你是否也指定了db name吗?
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
throw new Exception("No database");
}
String connectionURL = "jdbc:mysql://localhost:3306/dbname";
con = DriverManager.getConnection(connectionURL, "root", "password");