有几个类似主题的问题,但我做了彻底的调查,没有一个能解决我的问题。
我正在研究MVC Java项目。我使用Spring 3,Tomcat 8,MySQL 5和Hibernate 4.我手动创建了一个数据库,一个表和一行。我的Eclipse项目无法访问数据库,尽管一切似乎已经到位且配置正确。
这是我的基础DAO:
public class BaseDAOImpl<ENTITY> implements BaseDAO<ENTITY> {
private SessionFactory sessionFactory;
private Class<ENTITY> entityClass;
public BaseDAOImpl() {
this.entityClass = (Class<ENTITY>) ((ParameterizedType)
getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
// Other methods relying on the session //
@Override
public ENTITY createOrUpdate(ENTITY entity) {
Session session = getSession();
if (session==null) {
//throw new ConnectException();
}
session.saveOrUpdate(entity);
return null;
}
protected Session getSession() {
if (sessionFactory == null) {
initializeSessionFactory();
}
return this.sessionFactory.getCurrentSession();
}
public void initializeSessionFactory() {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());
}
}
和hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/sudomusic
</property>
<property name="hibernate.connection.port">
3307
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
kk
</property>
<!-- List of XML mapping files -->
<mapping class="com.evolusound.sudomusic.domain.Customer"/>
</session-factory>
</hibernate-configuration>
因此,当我尝试在数据库上保存新客户时,我收到了JDBC连接异常。最让我担忧的是:我是否在DAO中创建了sessionFactory? hibernate.cfg.xml中是否缺少任何内容?它应该自动映射我的Customer.java和Customer表,对吗?我不知道链是哪个链接丢失了,显然是连接,但我不知道在哪一步。是否配置不正确或首次尝试失败?任何帮助将不胜感激。
__
如果它有助于解决问题,Eclipse框架似乎能够使用mySQL连接使用Data Source Explorer查看数据库。但是,它无法看到其中的表格。我确信凭证是正确的(root,kk)因为我在命令行和MySQL Workbench中使用它们。一些帮助将非常感激。
__
这是控制台输出:
[Request processing failed; nested exception is org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect] with root cause
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:292)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2132)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
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:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at com.evolusound.sudomusic.data_access.BaseDAOImpl.initializeSessionFactory(BaseDAOImpl.java:73)
...
AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost/sudomusic]
答案 0 :(得分:0)
您的端口号是否正确? MySQL的默认端口是3306,但您在XML中指定了3307:
<property name="hibernate.connection.url">
jdbc:mysql://localhost/sudomusic
</property>
<property name="hibernate.connection.port">
3307
</property>
你的问题是你正试图在localhost:3306上连接到MySQL,但是你得到连接拒绝,可能是因为MySQL没有运行,或者因为端口错误。