学习dao,想不用每个方法到连接一次库,放到测试类的before里,报错了。看到提示没理解啥原因
代码
public class DaoTest {
public static SimpleDataSource dataSource = new SimpleDataSource();
public static Dao dao = new NutDao(dataSource);
@Before
public void setUp() throws Exception {
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1/nutzdemo");
dataSource.setUsername("root");
dataSource.setPassword("888888");
dao.create(Person.class, false);//drop if exists false
}
@Test
public void testA() {
Person person = new Person();
person.setAge(21);
person.setName("F");
dao.insert(person);
System.out.println(person.getId());
}
}
日志如下
[WARN ] 16:56:42.604 org.nutz.dao.impl.SimpleDataSource.<init>(SimpleDataSource.java:34) - SimpleDataSource is NOT a Connection Pool, So it is slow but safe for debug/study
[DEBUG] 16:56:42.820 org.nutz.castor.Castors.reload(Castors.java:115) - Using 95 castor for Castors
[DEBUG] 16:56:42.941 org.nutz.dao.jdbc.Jdbcs.<clinit>(Jdbcs.java:90) - Jdbcs init complete
[INFO ] 16:56:42.941 org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:103) - Get Connection from DataSource for JdbcExpert, if you lock at here, check your database server and configure
Disconnected from the target VM, address: '127.0.0.1:5128', transport: 'socket'
java.lang.ExceptionInInitializerError
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:423)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
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.RuntimeException: java.sql.SQLException: The url cannot be null
at org.nutz.lang.Lang.wrapThrow(Lang.java:185)
at org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:113)
at org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:187)
at org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:178)
at org.nutz.dao.impl.NutDao.<init>(NutDao.java:105)
at DaoTest.<clinit>(DaoTest.java:10)
... 21 more
Caused by: java.sql.SQLException: The url cannot be null
at java.sql.DriverManager.getConnection(DriverManager.java:649)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at org.nutz.dao.impl.SimpleDataSource.getConnection(SimpleDataSource.java:45)
at org.nutz.trans.Trans.getConnectionAuto(Trans.java:263)
at org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:106)
... 25 more
Process finished with exit code -1