为了账号安全,请及时绑定邮箱和手机立即绑定

不能正常运行

https://img1.sycdn.imooc.com//5b497a5a00015b9e16400515.jpg

https://img1.sycdn.imooc.com//5b497a1b00010a9519200827.jpg

https://img1.sycdn.imooc.com//5b497a360001174019200884.jpg

https://img1.sycdn.imooc.com//5b497a46000143cf11700196.jpg

https://img1.sycdn.imooc.com//5b497a6800010d1516620617.jpg

需要哪部分的代码或截图可以告诉我,请各位帮忙看一下,谢谢

正在回答

6 回答

建议写数据库连接语句这样会抛出异常的语句,最好写在try catch 里面,出了问题也能快速锁定

0 回复 有任何疑惑可以回复我~

package hibernate_Demo;



import java.util.Date;


import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.hibernate.service.ServiceRegistryBuilder;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;


public class StudentTest {

private SessionFactory sessionFactory;

private Session session;

private Transaction transaction;

@Before

public void init() {

// 创建配置对象

Configuration config = new Configuration().configure();

// 创建服务注册对象

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();

// 创建会话工厂对象

sessionFactory = config.buildSessionFactory(serviceRegistry);

// 会话对象

session = sessionFactory.openSession();

// 开启事务

transaction = session.beginTransaction();

}

@After

public void destory() {

// 提交事务

transaction.commit();

// 关闭会话

session.close();

// 关闭会话工厂

sessionFactory.close();

}


@Test

public void testSaveStudent() {

Students s = new Students(1,"11","11",new Date(),"1");

session.save(s);

}

}


0 回复 有任何疑惑可以回复我~
#1

qq_黑色Aquarius_0

你不回复我在自己帖子下写我哪看得到....... 代码看起来没问题,不过你那个异常信息中还有个时区连接错误,搜一下说,可能是你的mysql-jdbc版本高了,结果配置方式变了要在connection.url那个连接数据库语句后面拼接一个&serverTimezone=UTC https://blog.csdn.net/mjlfto/article/details/74906163
2018-07-14 回复 有任何疑惑可以回复我~
#2

qq_黑色Aquarius_0 回复 qq_黑色Aquarius_0

那么问题就可能是如下,你连接数据库时出了问题,抛出了异常,但你没有捕获这个异常导致程序接着往下走.然后以后没有获取到数据库连接,导致下面进行事务提交的时候抛出了空指针
2018-07-14 回复 有任何疑惑可以回复我~
#3

the__sky123 提问者 回复 qq_黑色Aquarius_0

加时区了org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml 和空指针异常
2018-07-14 回复 有任何疑惑可以回复我~
#4

qq_黑色Aquarius_0 回复 the__sky123 提问者

那可能是时区加的方法不对,你还是换个低点版本的mysql-jdbc吧,高版本的写法也不同,需要你自己研究
2018-07-14 回复 有任何疑惑可以回复我~
查看1条回复
以下是StudentTest。java
package hibernate_Demo;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;public class StudentTest {	private SessionFactory sessionFactory;	private Session session;	private Transaction transaction;	@Before	public void init() {		// 创建配置对象		Configuration config = new Configuration().configure();		// 创建服务注册对象		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();		// 创建会话工厂对象		sessionFactory = config.buildSessionFactory(serviceRegistry);		// 会话对象		session = sessionFactory.openSession();		// 开启事务		transaction = session.beginTransaction();	}		@After	public void destory() {		// 提交事务		transaction.commit();		// 关闭会话		session.close();		// 关闭会话工厂		sessionFactory.close();	}	@Test	public void testSaveStudent() {		Students s = new Students(1,"11","11",new Date(),"1");		session.save(s);	}		}


0 回复 有任何疑惑可以回复我~

org.hibernate.exception.GenericJDBCException: Could not open connection

at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)

at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)

at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)

at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)

at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)

at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)

at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)

at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)

at hibernate_Demo.StudentTest.init(StudentTest.java:31)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

at org.junit.runners.ParentRunner.run(ParentRunner.java:300)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

Caused by: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)

at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)

at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)

at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)

at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)

at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)

at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)

at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)

at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)

... 29 more

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)

at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)

at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)

at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)

at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)

at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)

at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993)

at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)

... 35 more


java.lang.NullPointerException

at hibernate_Demo.StudentTest.destory(StudentTest.java:37)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

at org.junit.runners.ParentRunner.run(ParentRunner.java:300)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)


0 回复 有任何疑惑可以回复我~
#1

qq_黑色Aquarius_0

这个就说明不是xml的问题了 Could not open connection 是连接不到数据库,要么数据配置不对或不存在,要么就是你在执行某个操作前关了连接 java.lang.NullPointerException 空指针异常,那个地方没有获取到对象,就在这一行at hibernate_Demo.StudentTest.destory(StudentTest.java:37)
2018-07-14 回复 有任何疑惑可以回复我~

可能是students xml的路径不对,你看是不是要放在同一文件夹下

0 回复 有任何疑惑可以回复我~
#1

the__sky123 提问者

要把两个xml放到一个文件下?
2018-07-14 回复 有任何疑惑可以回复我~
#2

qq_黑色Aquarius_0 回复 the__sky123 提问者

试一下看看,不在一个路径下可能找不到
2018-07-14 回复 有任何疑惑可以回复我~
#3

qq_黑色Aquarius_0 回复 qq_黑色Aquarius_0

放在别的路径下,resources要写不同相对路径,应该
2018-07-14 回复 有任何疑惑可以回复我~
#4

the__sky123 提问者 回复 qq_黑色Aquarius_0

不行,把他俩都放在一个文件夹下会报错了Configuration resource: /hibernate.cfg.xml
2018-07-14 回复 有任何疑惑可以回复我~
#5

qq_黑色Aquarius_0 回复 the__sky123 提问者

那要看你代码怎么写的了
2018-07-14 回复 有任何疑惑可以回复我~
#6

the__sky123 提问者 回复 qq_黑色Aquarius_0

需要看哪部分代码
2018-07-14 回复 有任何疑惑可以回复我~
#7

qq_黑色Aquarius_0 回复 the__sky123 提问者

你那个单元测试的
2018-07-14 回复 有任何疑惑可以回复我~
#8

the__sky123 提问者 回复 qq_黑色Aquarius_0

什么?
2018-07-14 回复 有任何疑惑可以回复我~
#9

qq_黑色Aquarius_0 回复 the__sky123 提问者

就是你那个test,不过看你报的错是路径不对,你也可以试一下在写resources="hibernate_Demo/Students.hbm.xml"
2018-07-14 回复 有任何疑惑可以回复我~
#10

the__sky123 提问者 回复 qq_黑色Aquarius_0

INFO: HHH000040: Configuration resource: /hibernate.cfg.xml成这样了
2018-07-14 回复 有任何疑惑可以回复我~
#11

qq_黑色Aquarius_0 回复 the__sky123 提问者

能不能搞个报错信息...INFO: HHH000040: Configuration resource: /hibernate.cfg.xml这根本就不是报错信息啊...,mappingnotfound就是找不到文件异常,这个不是你eclipse抽风就是你哪路径写错了.你可以捕捉一下异常,断点跟踪一下
2018-07-14 回复 有任何疑惑可以回复我~
#12

qq_黑色Aquarius_0 回复 qq_黑色Aquarius_0

如果是缓存问题就clean一下项目
2018-07-14 回复 有任何疑惑可以回复我~
#13

the__sky123 提问者 回复 qq_黑色Aquarius_0

报错信息在哪看
2018-07-14 回复 有任何疑惑可以回复我~
#14

the__sky123 提问者 回复 qq_黑色Aquarius_0

找到了
2018-07-14 回复 有任何疑惑可以回复我~
查看11条回复

举报

0/150
提交
取消

不能正常运行

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信