NutzCN Logo
问答 H2数据库嵌入的问题
发布于 2747天前 作者 ahrvk 2510 次浏览 复制 上一个帖子 下一个帖子
标签:

配置文件如下:

	fields : {
	    driverClass : 'org.h2.Driver',
	    jdbcUrl : 'jdbc:h2:db/nutz_right',                                                   
	    user : 'sa',
	    password : ''
	  }
  },

为什么不会自己生成H2的数据库文件?

13 回复

相对路径,你找不到而已

@wendal 相对路径应该是项目根目录吧,整个项目都没搜到。

是eclipse的安装目录

@wendal 这是为啥呢,基础不好啊,要生成在项目下面要怎么设置啊。

写绝对路径完事

@wendal 但是要部署到linux上去,到时候还要改啊。

那就这样写吧

jdbcUrl : {java : "aaa.bbb.ccc.Toolkit.getJdbcUrl('db/nutz_right')",

然后写个Toolkit类

public class Toolkit {
           public static String getJdbcUrl(String path) {
                  return "jdbc:h2:"+ Mvcs.getServletContext().getRealpath("/") + path;
           }
}

@wendal 生成的是这个 jdbc:h2:E:\WorkSpace\neon-3-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\nutz-right\db/nutz_right

是的,web项目嘛

@wendal 下一步又出现问题了
日志如下:

14:49:00.105 [localhost-startStop-1] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@5a6d38bc [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1f55ded7)
14:49:00.105 [localhost-startStop-1] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Driver --> 1.3.172 (2013-05-25)
14:49:00.105 [localhost-startStop-1] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Name   --> H2 JDBC Driver
14:49:00.105 [localhost-startStop-1] DEBUG org.nutz.dao.impl.DaoSupport - JDBC URL    --> jdbc:h2:file:~/db/nutz_right
14:49:00.105 [localhost-startStop-1] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@5a6d38bc [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1f55ded7)
14:49:00.105 [localhost-startStop-1] DEBUG org.nutz.dao.impl.DaoSupport - Database info --> H2:[H2 - 1.3.172 (2013-05-25)]
14:49:00.105 [localhost-startStop-1] DEBUG org.nutz.resource.Scans - Found 8 resource by src( com/weirhp/domain/ ) , regex( ^.+[.]class$ )
14:49:00.152 [localhost-startStop-1] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@5a6d38bc [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1f55ded7)
14:49:00.168 [localhost-startStop-1] DEBUG com.mchange.v2.c3p0.impl.NewPooledConnection - com.mchange.v2.c3p0.impl.NewPooledConnection@1f55ded7 handling a throwable.
org.h2.jdbc.JdbcSQLException: Table "MENU" not found; SQL statement:
SELECT * FROM menu LIMIT 1 [42102-172]
	at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.message.DbException.get(DbException.java:169) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.message.DbException.get(DbException.java:146) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.readTableOrView(Parser.java:4824) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.readTableFilter(Parser.java:1099) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1705) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelectSimple(Parser.java:1813) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelectSub(Parser.java:1699) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelectUnion(Parser.java:1542) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelect(Parser.java:1530) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parsePrepared(Parser.java:405) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parse(Parser.java:279) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parse(Parser.java:251) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.prepareCommand(Parser.java:218) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.engine.Session.prepareLocal(Session.java:425) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.engine.Session.prepareCommand(Session.java:374) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:71) ~[h2-1.3.172.jar:1.3.172]
	at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery(NewProxyStatement.java:35) ~[c3p0-0.9.1.2.jar:0.9.1.2]
	at org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:80) [nutz-1.r.60.jar:na]
	at org.nutz.dao.impl.entity.AnnotationEntityMaker._checkupEntityFieldsWithDatabase(AnnotationEntityMaker.java:552) [nutz-1.r.60.jar:na]
	at org.nutz.dao.impl.entity.AnnotationEntityMaker.make(AnnotationEntityMaker.java:278) [nutz-1.r.60.jar:na]
	at org.nutz.dao.impl.EntityHolder.getEntity(EntityHolder.java:71) [nutz-1.r.60.jar:na]
	at org.nutz.dao.impl.NutDao.getEntity(NutDao.java:655) [nutz-1.r.60.jar:na]
	at org.nutz.dao.util.Daos.createTablesInPackage(Daos.java:525) [nutz-1.r.60.jar:na]
	at com.weirhp.web.MvcSetup.init(MvcSetup.java:24) [MvcSetup.class:na]
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:277) [nutz-1.r.60.jar:na]
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121) [nutz-1.r.60.jar:na]
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19) [nutz-1.r.60.jar:na]
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87) [nutz-1.r.60.jar:na]
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65) [nutz-1.r.60.jar:na]
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) [catalina.jar:7.0.61]
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) [catalina.jar:7.0.61]
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) [catalina.jar:7.0.61]
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4854) [catalina.jar:7.0.61]
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5546) [catalina.jar:7.0.61]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.61]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) [catalina.jar:7.0.61]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) [catalina.jar:7.0.61]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_121]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
14:49:00.168 [localhost-startStop-1] DEBUG com.mchange.v2.sql.SqlUtils - Attempted to convert SQLException to SQLException. Leaving it alone. [SQLState: 42S02; errorCode: 42102]
org.h2.jdbc.JdbcSQLException: Table "MENU" not found; SQL statement:
SELECT * FROM menu LIMIT 1 [42102-172]
	at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.message.DbException.get(DbException.java:169) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.message.DbException.get(DbException.java:146) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.readTableOrView(Parser.java:4824) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.readTableFilter(Parser.java:1099) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1705) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelectSimple(Parser.java:1813) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelectSub(Parser.java:1699) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelectUnion(Parser.java:1542) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parseSelect(Parser.java:1530) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parsePrepared(Parser.java:405) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parse(Parser.java:279) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.parse(Parser.java:251) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.command.Parser.prepareCommand(Parser.java:218) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.engine.Session.prepareLocal(Session.java:425) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.engine.Session.prepareCommand(Session.java:374) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138) ~[h2-1.3.172.jar:1.3.172]
	at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:71) ~[h2-1.3.172.jar:1.3.172]
	at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery(NewProxyStatement.java:35) ~[c3p0-0.9.1.2.jar:0.9.1.2]
	at org.nutz.dao.impl.jdbc.AbstractJdbcExpert.setupEntityField(AbstractJdbcExpert.java:80) [nutz-1.r.60.jar:na]
	at org.nutz.dao.impl.entity.AnnotationEntityMaker._checkupEntityFieldsWithDatabase(AnnotationEntityMaker.java:552) [nutz-1.r.60.jar:na]
	at org.nutz.dao.impl.entity.AnnotationEntityMaker.make(AnnotationEntityMaker.java:278) [nutz-1.r.60.jar:na]
	at org.nutz.dao.impl.EntityHolder.getEntity(EntityHolder.java:71) [nutz-1.r.60.jar:na]
	at org.nutz.dao.impl.NutDao.getEntity(NutDao.java:655) [nutz-1.r.60.jar:na]
	at org.nutz.dao.util.Daos.createTablesInPackage(Daos.java:525) [nutz-1.r.60.jar:na]
	at com.weirhp.web.MvcSetup.init(MvcSetup.java:24) [MvcSetup.class:na]
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:277) [nutz-1.r.60.jar:na]
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121) [nutz-1.r.60.jar:na]
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19) [nutz-1.r.60.jar:na]
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87) [nutz-1.r.60.jar:na]
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65) [nutz-1.r.60.jar:na]
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) [catalina.jar:7.0.61]
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) [catalina.jar:7.0.61]
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) [catalina.jar:7.0.61]
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4854) [catalina.jar:7.0.61]
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5546) [catalina.jar:7.0.61]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.61]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) [catalina.jar:7.0.61]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) [catalina.jar:7.0.61]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_121]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

表不存在,还有啥好说的

@wendal 我是自动创建表的

 public void init(NutConfig nc) {
        log.debug("I start!!");
          
//        H2Server.startH2Server();
        
        Ioc ioc = nc.getIoc();
        Dao dao = ioc.get(Dao.class);
        
        Daos.createTablesInPackage(dao, "com.weirhp.domain", false);
        
    }

c3p0乱报的吧

添加回复
请先登陆
回到顶部