不能正常运行

来源:1-10 使用JUnit进行测试

the__sky123

2018-07-14 12:22

https://img4.mukewang.com/5b497a5a00015b9e16400515.jpg

https://img.mukewang.com/5b497a1b00010a9519200827.jpg

https://img4.mukewang.com/5b497a360001174019200884.jpg

https://img.mukewang.com/5b497a46000143cf11700196.jpg

https://img2.mukewang.com/5b497a6800010d1516620617.jpg

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

写回答 关注

5回答

  • qq_黑色Aquarius_0
    2018-07-14 18:12:09

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

  • the__sky123
    2018-07-14 16:57:51

    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);

    }

    }


    qq_黑色A... 回复the__s...

    那可能是时区加的方法不对,你还是换个低点版本的mysql-jdbc吧,高版本的写法也不同,需要你自己研究

    2018-07-14 18:10:55

    共 4 条回复 >

  • the__sky123
    2018-07-14 16:57:01
    以下是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);	}		}


  • the__sky123
    2018-07-14 16:48:09

    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)


    qq_黑色A...

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

    2018-07-14 16:52:30

    共 1 条回复 >

  • qq_黑色Aquarius_0
    2018-07-14 15:19:12

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

    the__s... 回复qq_黑色A...

    找到了

    2018-07-14 16:45:56

    共 14 条回复 >

Hibernate初探之单表映射

Java持久化框架Hibernate入门教程,掌握Hibernate基本概念

74806 学习 · 793 问题

查看课程

相似问题