代码运行报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.imooc.os.dao; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class OrderTest { private String driver = "com.mysql.jdbc.Driver" ; private String url = "jdbc:mysql://localhost:3306/os?useUnicode=true&characterEncoding=utf8" ; private String userName = "root" ; private String password = "dbweitong123" ; @Test public void addOrder(){ try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } Connection connection = null ; try { connection = DriverManager.getConnection(url,userName,password); connection.setAutoCommit( false ); Statement statement = connection.createStatement(); statement.execute( "insert into orders values('100002','100001',2,2499,now(),null,null,'刘备','1330000000','成都','待发货')" ); statement.execute( "update products set stck=stock-2 where id='100001'" ); statement.close(); connection.commit(); } catch (SQLException e) { e.printStackTrace(); try { connection.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } |
报错信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 62 ) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java: 45 ) at java.lang.reflect.Constructor.newInstance(Constructor.java: 422 ) at com.mysql.jdbc.Util.handleNewInstance(Util.java: 404 ) at com.mysql.jdbc.Util.getInstance(Util.java: 387 ) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java: 919 ) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java: 898 ) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java: 887 ) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java: 862 ) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java: 2331 ) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java: 2084 ) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java: 795 ) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java: 44 ) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 62 ) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java: 45 ) at java.lang.reflect.Constructor.newInstance(Constructor.java: 422 ) at com.mysql.jdbc.Util.handleNewInstance(Util.java: 404 ) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java: 400 ) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java: 327 ) at java.sql.DriverManager.getConnection(DriverManager.java: 664 ) at java.sql.DriverManager.getConnection(DriverManager.java: 247 ) at com.imooc.os.dao.OrderTest.addOrder(OrderTest.java: 25 ) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 62 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43 ) at java.lang.reflect.Method.invoke(Method.java: 497 ) at org.junit.runners.model.FrameworkMethod$ 1 .runReflectiveCall(FrameworkMethod.java: 50 ) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java: 12 ) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java: 47 ) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java: 17 ) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java: 325 ) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java: 78 ) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java: 57 ) at org.junit.runners.ParentRunner$ 3 .run(ParentRunner.java: 290 ) at org.junit.runners.ParentRunner$ 1 .schedule(ParentRunner.java: 71 ) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java: 288 ) at org.junit.runners.ParentRunner.access$ 000 (ParentRunner.java: 58 ) at org.junit.runners.ParentRunner$ 2 .evaluate(ParentRunner.java: 268 ) at org.junit.runners.ParentRunner.run(ParentRunner.java: 363 ) at org.junit.runner.JUnitCore.run(JUnitCore.java: 137 ) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java: 68 ) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java: 47 ) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java: 242 ) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java: 70 ) Caused by: java.lang.NullPointerException at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java: 3004 ) at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java: 1908 ) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java: 1837 ) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java: 1207 ) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java: 2254 ) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java: 2285 ) ... 35 more java.lang.NullPointerException at com.imooc.os.dao.OrderTest.addOrder(OrderTest.java: 41 ) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 62 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43 ) at java.lang.reflect.Method.invoke(Method.java: 497 ) at org.junit.runners.model.FrameworkMethod$ 1 .runReflectiveCall(FrameworkMethod.java: 50 ) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java: 12 ) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java: 47 ) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java: 17 ) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java: 325 ) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java: 78 ) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java: 57 ) at org.junit.runners.ParentRunner$ 3 .run(ParentRunner.java: 290 ) at org.junit.runners.ParentRunner$ 1 .schedule(ParentRunner.java: 71 ) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java: 288 ) at org.junit.runners.ParentRunner.access$ 000 (ParentRunner.java: 58 ) at org.junit.runners.ParentRunner$ 2 .evaluate(ParentRunner.java: 268 ) at org.junit.runners.ParentRunner.run(ParentRunner.java: 363 ) at org.junit.runner.JUnitCore.run(JUnitCore.java: 137 ) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java: 68 ) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java: 47 ) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java: 242 ) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java: 70 ) |
0
收起
正在回答 回答被采纳积分+1
1回答
SSM主流框架入门与综合项目实战2018版
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧